@@ -658,43 +658,44 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
658658 addLibraries (SYCLDeviceAnnotationLibs);
659659
660660#if !defined(_WIN32)
661+ std::string SanitizeVal;
661662 size_t sanitizer_lib_idx = getSingleBuildTarget ();
662663 if (Arg *A = Args.getLastArg (options::OPT_fsanitize_EQ,
663664 options::OPT_fno_sanitize_EQ)) {
664665 if (A->getOption ().matches (options::OPT_fsanitize_EQ) &&
665- A->getValues ().size () == 1 ) {
666- std::string SanitizeVal = A->getValue ();
667- if (SanitizeVal == " address" )
668- addSingleLibrary (SYCLDeviceAsanLibs[sanitizer_lib_idx]);
669- }
666+ A->getValues ().size () == 1 )
667+ SanitizeVal = A->getValue ();
670668 } else {
671669 // User can pass -fsanitize=address to device compiler via
672670 // -Xsycl-target-frontend, sanitize device library must be
673671 // linked with user's device image if so.
674- bool IsDeviceAsanEnabled = false ;
675- auto SyclFEArg = Args.getAllArgValues (options::OPT_Xsycl_frontend);
676- IsDeviceAsanEnabled = (std::count (SyclFEArg.begin (), SyclFEArg.end (),
677- " -fsanitize=address" ) > 0 );
678- if (!IsDeviceAsanEnabled) {
679- auto SyclFEArgEq = Args.getAllArgValues (options::OPT_Xsycl_frontend_EQ);
680- IsDeviceAsanEnabled = (std::count (SyclFEArgEq.begin (), SyclFEArgEq.end (),
681- " -fsanitize=address" ) > 0 );
682- }
683-
684- // User can also enable asan for SYCL device via -Xarch_device option.
685- if (!IsDeviceAsanEnabled) {
686- auto DeviceArchVals = Args.getAllArgValues (options::OPT_Xarch_device);
687- for (auto DArchVal : DeviceArchVals) {
688- if (DArchVal.find (" -fsanitize=address" ) != std::string::npos) {
689- IsDeviceAsanEnabled = true ;
690- break ;
691- }
672+ std::vector<std::string> EnabledDeviceSanitizers;
673+
674+ // NOTE: "-fsanitize=" applies to all device targets
675+ auto SyclFEArgVals = Args.getAllArgValues (options::OPT_Xsycl_frontend);
676+ auto SyclFEEQArgVals = Args.getAllArgValues (options::OPT_Xsycl_frontend_EQ);
677+ auto ArchDeviceVals = Args.getAllArgValues (options::OPT_Xarch_device);
678+
679+ std::vector<std::string> ArgVals (
680+ SyclFEArgVals.size () + SyclFEEQArgVals.size () + ArchDeviceVals.size ());
681+ ArgVals.insert (ArgVals.end (), SyclFEArgVals.begin (), SyclFEArgVals.end ());
682+ ArgVals.insert (ArgVals.end (), SyclFEEQArgVals.begin (),
683+ SyclFEEQArgVals.end ());
684+ ArgVals.insert (ArgVals.end (), ArchDeviceVals.begin (), ArchDeviceVals.end ());
685+
686+ // Driver will report error if address sanitizer and memory sanitizer are
687+ // both enabled, so we only need to check first one here.
688+ for (const std::string &Arg : ArgVals) {
689+ if (Arg.find (" -fsanitize=address" ) != std::string::npos) {
690+ SanitizeVal = " address" ;
691+ break ;
692692 }
693693 }
694-
695- if (IsDeviceAsanEnabled)
696- addSingleLibrary (SYCLDeviceAsanLibs[sanitizer_lib_idx]);
697694 }
695+
696+ if (SanitizeVal == " address" )
697+ addSingleLibrary (SYCLDeviceAsanLibs[sanitizer_lib_idx]);
698+
698699#endif
699700
700701 if (isNativeCPU)
0 commit comments