@@ -426,33 +426,6 @@ static void truncateCurrentFile(void) {
426
426
fclose (File );
427
427
}
428
428
429
- #if !defined(__Fuchsia__ ) && !defined(_WIN32 )
430
- static void assertIsZero (int * i ) {
431
- if (* i )
432
- PROF_WARN ("Expected flag to be 0, but got: %d\n" , * i );
433
- }
434
-
435
- /* Write a partial profile to \p Filename, which is required to be backed by
436
- * the open file object \p File. */
437
- static int writeProfileWithFileObject (const char * Filename , FILE * File ) {
438
- setProfileFile (File );
439
- int rc = writeFile (Filename );
440
- if (rc )
441
- PROF_ERR ("Failed to write file \"%s\": %s\n" , Filename , strerror (errno ));
442
- setProfileFile (NULL );
443
- return rc ;
444
- }
445
-
446
- /* Unlock the profile \p File and clear the unlock flag. */
447
- static void unlockProfile (int * ProfileRequiresUnlock , FILE * File ) {
448
- if (!* ProfileRequiresUnlock ) {
449
- PROF_WARN ("%s" , "Expected to require profile unlock\n" );
450
- }
451
- lprofUnlockFileHandle (File );
452
- * ProfileRequiresUnlock = 0 ;
453
- }
454
- #endif // !defined(__Fuchsia__) && !defined(_WIN32)
455
-
456
429
static int writeMMappedFile (FILE * OutputFile , char * * Profile ) {
457
430
if (!OutputFile )
458
431
return -1 ;
@@ -481,83 +454,38 @@ static int writeMMappedFile(FILE *OutputFile, char **Profile) {
481
454
return 0 ;
482
455
}
483
456
484
- static void relocateCounters (void ) {
485
- if (!__llvm_profile_is_continuous_mode_enabled () ||
486
- !lprofRuntimeCounterRelocation ())
487
- return ;
488
-
489
- /* Get the sizes of various profile data sections. Taken from
490
- * __llvm_profile_get_size_for_buffer(). */
491
- const __llvm_profile_data * DataBegin = __llvm_profile_begin_data ();
492
- const __llvm_profile_data * DataEnd = __llvm_profile_end_data ();
493
- const uint64_t * CountersBegin = __llvm_profile_begin_counters ();
494
- const uint64_t * CountersEnd = __llvm_profile_end_counters ();
495
- uint64_t DataSize = __llvm_profile_get_data_size (DataBegin , DataEnd );
496
- const uint64_t CountersOffset = sizeof (__llvm_profile_header ) +
497
- (DataSize * sizeof (__llvm_profile_data ));
498
-
499
- int Length = getCurFilenameLength ();
500
- char * FilenameBuf = (char * )COMPILER_RT_ALLOCA (Length + 1 );
501
- const char * Filename = getCurFilename (FilenameBuf , 0 );
502
- if (!Filename )
503
- return ;
504
-
505
- FILE * File = NULL ;
506
- char * Profile = NULL ;
507
-
508
- if (!doMerging ()) {
509
- File = fopen (Filename , "w+b" );
510
- if (!File )
511
- return ;
512
-
513
- if (writeMMappedFile (File , & Profile ) == -1 ) {
514
- fclose (File );
515
- return ;
516
- }
517
- } else {
518
- File = lprofOpenFileEx (Filename );
519
- if (!File )
520
- return ;
521
-
522
- uint64_t ProfileFileSize = 0 ;
523
- if (getProfileFileSizeForMerging (File , & ProfileFileSize ) == -1 ) {
524
- lprofUnlockFileHandle (File );
525
- fclose (File );
526
- return ;
527
- }
457
+ // TODO: Move these functions into InstrProfilingPlatform* files.
458
+ #if defined(__APPLE__ )
459
+ static void assertIsZero (int * i ) {
460
+ if (* i )
461
+ PROF_WARN ("Expected flag to be 0, but got: %d\n" , * i );
462
+ }
528
463
529
- if (!ProfileFileSize ) {
530
- if (writeMMappedFile (File , & Profile ) == -1 ) {
531
- fclose (File );
532
- return ;
533
- }
534
- } else {
535
- /* The merged profile has a non-zero length. Check that it is compatible
536
- * with the data in this process. */
537
- if (mmapProfileForMerging (File , ProfileFileSize , & Profile ) == -1 ) {
538
- fclose (File );
539
- return ;
540
- }
541
- }
464
+ /* Write a partial profile to \p Filename, which is required to be backed by
465
+ * the open file object \p File. */
466
+ static int writeProfileWithFileObject (const char * Filename , FILE * File ) {
467
+ setProfileFile (File );
468
+ int rc = writeFile (Filename );
469
+ if (rc )
470
+ PROF_ERR ("Failed to write file \"%s\": %s\n" , Filename , strerror (errno ));
471
+ setProfileFile (NULL );
472
+ return rc ;
473
+ }
542
474
543
- lprofUnlockFileHandle (File );
475
+ /* Unlock the profile \p File and clear the unlock flag. */
476
+ static void unlockProfile (int * ProfileRequiresUnlock , FILE * File ) {
477
+ if (!* ProfileRequiresUnlock ) {
478
+ PROF_WARN ("%s" , "Expected to require profile unlock\n" );
544
479
}
545
480
546
- /* Update the profile fields based on the current mapping. */
547
- __llvm_profile_counter_bias =
548
- (intptr_t )Profile - (uintptr_t )CountersBegin + CountersOffset ;
549
-
550
- /* Return the memory allocated for counters to OS. */
551
- lprofReleaseMemoryPagesToOS ((uintptr_t )CountersBegin , (uintptr_t )CountersEnd );
481
+ lprofUnlockFileHandle (File );
482
+ * ProfileRequiresUnlock = 0 ;
552
483
}
553
484
554
485
static void initializeProfileForContinuousMode (void ) {
555
486
if (!__llvm_profile_is_continuous_mode_enabled ())
556
487
return ;
557
488
558
- #if defined(__Fuchsia__ ) || defined(_WIN32 )
559
- PROF_ERR ("%s\n" , "Continuous mode not yet supported on Fuchsia or Windows." );
560
- #else // defined(__Fuchsia__) || defined(_WIN32)
561
489
/* Get the sizes of various profile data sections. Taken from
562
490
* __llvm_profile_get_size_for_buffer(). */
563
491
const __llvm_profile_data * DataBegin = __llvm_profile_begin_data ();
@@ -683,8 +611,109 @@ static void initializeProfileForContinuousMode(void) {
683
611
684
612
if (ProfileRequiresUnlock )
685
613
unlockProfile (& ProfileRequiresUnlock , File );
686
- #endif // defined(__Fuchsia__) || defined(_WIN32)
687
614
}
615
+ #elif defined(__ELF__ ) || defined(_WIN32 )
616
+
617
+ #define INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR \
618
+ INSTR_PROF_CONCAT(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR, _default)
619
+ intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR = 0 ;
620
+
621
+ /* This variable is a weak external reference which could be used to detect
622
+ * whether or not the compiler defined this symbol. */
623
+ #if defined(_WIN32 )
624
+ COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR ;
625
+ #pragma comment(linker, "/alternatename:" \
626
+ INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR) "=" \
627
+ INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR))
628
+ #else
629
+ COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR
630
+ __attribute__((weak , alias (INSTR_PROF_QUOTE (
631
+ INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR )))) ;
632
+ #endif
633
+
634
+ static void initializeProfileForContinuousMode (void ) {
635
+ if (!__llvm_profile_is_continuous_mode_enabled ())
636
+ return ;
637
+
638
+ /* This symbol is defined by the compiler when runtime counter relocation is
639
+ * used and runtime provides a weak alias so we can check if it's defined. */
640
+ void * BiasAddr = & INSTR_PROF_PROFILE_COUNTER_BIAS_VAR ;
641
+ void * BiasDefaultAddr = & INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR ;
642
+ if (BiasAddr == BiasDefaultAddr ) {
643
+ PROF_ERR ("%s\n" , "__llvm_profile_counter_bias is undefined" );
644
+ return ;
645
+ }
646
+
647
+ /* Get the sizes of various profile data sections. Taken from
648
+ * __llvm_profile_get_size_for_buffer(). */
649
+ const __llvm_profile_data * DataBegin = __llvm_profile_begin_data ();
650
+ const __llvm_profile_data * DataEnd = __llvm_profile_end_data ();
651
+ const uint64_t * CountersBegin = __llvm_profile_begin_counters ();
652
+ const uint64_t * CountersEnd = __llvm_profile_end_counters ();
653
+ uint64_t DataSize = __llvm_profile_get_data_size (DataBegin , DataEnd );
654
+ const uint64_t CountersOffset =
655
+ sizeof (__llvm_profile_header ) + (DataSize * sizeof (__llvm_profile_data ));
656
+
657
+ int Length = getCurFilenameLength ();
658
+ char * FilenameBuf = (char * )COMPILER_RT_ALLOCA (Length + 1 );
659
+ const char * Filename = getCurFilename (FilenameBuf , 0 );
660
+ if (!Filename )
661
+ return ;
662
+
663
+ FILE * File = NULL ;
664
+ char * Profile = NULL ;
665
+
666
+ if (!doMerging ()) {
667
+ File = fopen (Filename , "w+b" );
668
+ if (!File )
669
+ return ;
670
+
671
+ if (writeMMappedFile (File , & Profile ) == -1 ) {
672
+ fclose (File );
673
+ return ;
674
+ }
675
+ } else {
676
+ File = lprofOpenFileEx (Filename );
677
+ if (!File )
678
+ return ;
679
+
680
+ uint64_t ProfileFileSize = 0 ;
681
+ if (getProfileFileSizeForMerging (File , & ProfileFileSize ) == -1 ) {
682
+ lprofUnlockFileHandle (File );
683
+ fclose (File );
684
+ return ;
685
+ }
686
+
687
+ if (!ProfileFileSize ) {
688
+ if (writeMMappedFile (File , & Profile ) == -1 ) {
689
+ fclose (File );
690
+ return ;
691
+ }
692
+ } else {
693
+ /* The merged profile has a non-zero length. Check that it is compatible
694
+ * with the data in this process. */
695
+ if (mmapProfileForMerging (File , ProfileFileSize , & Profile ) == -1 ) {
696
+ fclose (File );
697
+ return ;
698
+ }
699
+ }
700
+
701
+ lprofUnlockFileHandle (File );
702
+ }
703
+
704
+ /* Update the profile fields based on the current mapping. */
705
+ INSTR_PROF_PROFILE_COUNTER_BIAS_VAR =
706
+ (intptr_t )Profile - (uintptr_t )CountersBegin +
707
+ CountersOffset ;
708
+
709
+ /* Return the memory allocated for counters to OS. */
710
+ lprofReleaseMemoryPagesToOS ((uintptr_t )CountersBegin , (uintptr_t )CountersEnd );
711
+ }
712
+ #else
713
+ static void initializeProfileForContinuousMode (void ) {
714
+ PROF_ERR ("%s\n" , "continuous mode is unsupported on this platform" );
715
+ }
716
+ #endif
688
717
689
718
static const char * DefaultProfileName = "default.profraw" ;
690
719
static void resetFilenameToDefault (void ) {
@@ -784,9 +813,14 @@ static int parseFilenamePattern(const char *FilenamePat,
784
813
FilenamePat );
785
814
return -1 ;
786
815
}
787
-
816
+ #if defined( __APPLE__ ) || defined( __ELF__ ) || defined( _WIN32 )
788
817
__llvm_profile_set_page_size (getpagesize ());
789
818
__llvm_profile_enable_continuous_mode ();
819
+ #else
820
+ PROF_WARN ("%s" , "Continous mode is currently only supported for Mach-O,"
821
+ " ELF and COFF formats." );
822
+ return -1 ;
823
+ #endif
790
824
} else {
791
825
unsigned MergePoolSize = getMergePoolSize (FilenamePat , & I );
792
826
if (!MergePoolSize )
@@ -843,12 +877,8 @@ static void parseAndSetFilename(const char *FilenamePat,
843
877
}
844
878
845
879
truncateCurrentFile ();
846
- if (__llvm_profile_is_continuous_mode_enabled ()) {
847
- if (lprofRuntimeCounterRelocation ())
848
- relocateCounters ();
849
- else
850
- initializeProfileForContinuousMode ();
851
- }
880
+ if (__llvm_profile_is_continuous_mode_enabled ())
881
+ initializeProfileForContinuousMode ();
852
882
}
853
883
854
884
/* Return buffer length that is required to store the current profile
@@ -1004,9 +1034,6 @@ void __llvm_profile_initialize_file(void) {
1004
1034
ProfileNameSpecifier PNS = PNS_unknown ;
1005
1035
int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR [0 ] != 0 );
1006
1036
1007
- if (__llvm_profile_counter_bias != -1 )
1008
- lprofSetRuntimeCounterRelocation (1 );
1009
-
1010
1037
EnvFilenamePat = getFilenamePatFromEnv ();
1011
1038
if (EnvFilenamePat ) {
1012
1039
/* Pass CopyFilenamePat = 1, to ensure that the filename would be valid
0 commit comments