@@ -730,6 +730,71 @@ MemorySanitizerOptions::MemorySanitizerOptions(int TO, bool R, bool K,
730730 Recover(getOptOrDefault(ClKeepGoing, Kernel || R)),
731731 EagerChecks(getOptOrDefault(ClEagerChecks, EagerChecks)) {}
732732
733+ static StringMap<GlobalVariable *> GlobalStringMap;
734+
735+ GlobalVariable *getOrCreateGlobalString (Module &M, StringRef Name,
736+ StringRef Value,
737+ unsigned AddressSpace) {
738+ GlobalVariable *StringGV = nullptr ;
739+ if (GlobalStringMap.find (Value.str ()) != GlobalStringMap.end ())
740+ return GlobalStringMap.at (Value.str ());
741+
742+ auto *Ty = ArrayType::get (Type::getInt8Ty (M.getContext ()), Value.size () + 1 );
743+ StringGV = new GlobalVariable (
744+ M, Ty, true , GlobalValue::InternalLinkage,
745+ ConstantDataArray::getString (M.getContext (), Value), Name, nullptr ,
746+ GlobalValue::NotThreadLocal, AddressSpace);
747+ GlobalStringMap[Value.str ()] = StringGV;
748+
749+ return StringGV;
750+ }
751+
752+ static void extendSpirKernelArgs (Module &M) {
753+ SmallVector<Constant *, 8 > SpirKernelsMetadata;
754+
755+ auto DL = M.getDataLayout ();
756+ Type *IntptrTy = DL.getIntPtrType (M.getContext ());
757+
758+ // SpirKernelsMetadata only saves fixed kernels, and is described by
759+ // following structure:
760+ // uptr unmangled_kernel_name
761+ // uptr unmangled_kernel_name_size
762+ StructType *StructTy = StructType::get (IntptrTy, IntptrTy);
763+ for (Function &F : M) {
764+ if (F.getCallingConv () != CallingConv::SPIR_KERNEL)
765+ continue ;
766+
767+ if (!F.hasFnAttribute (Attribute::SanitizeMemory) ||
768+ F.hasFnAttribute (Attribute::DisableSanitizerInstrumentation))
769+ continue ;
770+
771+ auto KernelName = F.getName ();
772+ auto *KernelNameGV = getOrCreateGlobalString (M, " __msan_kernel" , KernelName,
773+ kSpirOffloadGlobalAS );
774+ SpirKernelsMetadata.emplace_back (ConstantStruct::get (
775+ StructTy, ConstantExpr::getPointerCast (KernelNameGV, IntptrTy),
776+ ConstantInt::get (IntptrTy, KernelName.size ())));
777+ }
778+
779+ // Create global variable to record spirv kernels' information
780+ ArrayType *ArrayTy = ArrayType::get (StructTy, SpirKernelsMetadata.size ());
781+ Constant *MetadataInitializer =
782+ ConstantArray::get (ArrayTy, SpirKernelsMetadata);
783+ GlobalVariable *MsanSpirKernelMetadata = new GlobalVariable (
784+ M, MetadataInitializer->getType (), false , GlobalValue::AppendingLinkage,
785+ MetadataInitializer, " __MsanKernelMetadata" , nullptr ,
786+ GlobalValue::NotThreadLocal, 1 );
787+ MsanSpirKernelMetadata->setUnnamedAddr (GlobalValue::UnnamedAddr::Local);
788+ // Add device global attributes
789+ MsanSpirKernelMetadata->addAttribute (
790+ " sycl-device-global-size" , std::to_string (DL.getTypeAllocSize (ArrayTy)));
791+ MsanSpirKernelMetadata->addAttribute (" sycl-device-image-scope" );
792+ MsanSpirKernelMetadata->addAttribute (" sycl-host-access" , " 0" ); // read only
793+ MsanSpirKernelMetadata->addAttribute (" sycl-unique-id" ,
794+ " _Z20__MsanKernelMetadata" );
795+ MsanSpirKernelMetadata->setDSOLocal (true );
796+ }
797+
733798PreservedAnalyses MemorySanitizerPass::run (Module &M,
734799 ModuleAnalysisManager &AM) {
735800 // Return early if nosanitize_memory module flag is present for the module.
@@ -743,6 +808,11 @@ PreservedAnalyses MemorySanitizerPass::run(Module &M,
743808 Modified = true ;
744809 }
745810
811+ if (TargetTriple.isSPIROrSPIRV ()) {
812+ extendSpirKernelArgs (M);
813+ Modified = true ;
814+ }
815+
746816 auto &FAM = AM.getResult <FunctionAnalysisManagerModuleProxy>(M).getManager ();
747817 for (Function &F : M) {
748818 if (F.empty ())
@@ -752,6 +822,10 @@ PreservedAnalyses MemorySanitizerPass::run(Module &M,
752822 Msan.sanitizeFunction (F, FAM.getResult <TargetLibraryAnalysis>(F));
753823 }
754824
825+ // Clear GlobalStringMap to prevent its content from being used by other
826+ // modules
827+ GlobalStringMap.clear ();
828+
755829 if (!Modified)
756830 return PreservedAnalyses::all ();
757831
@@ -1276,7 +1350,6 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
12761350 SmallVector<std::pair<IntrinsicInst *, AllocaInst *>, 16 > LifetimeStartList;
12771351 SmallVector<StoreInst *, 16 > StoreList;
12781352 int64_t SplittableBlocksCount = 0 ;
1279- StringMap<GlobalVariable *> GlobalStringMap;
12801353
12811354 MemorySanitizerVisitor (Function &F, MemorySanitizer &MS,
12821355 const TargetLibraryInfo &TLI)
@@ -1474,24 +1547,6 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
14741547 return LazyWarningDebugLocationCount[DebugLoc] >= ClDisambiguateWarning;
14751548 }
14761549
1477- GlobalVariable *GetOrCreateGlobalString (Module &M, StringRef Name,
1478- StringRef Value,
1479- unsigned AddressSpace) {
1480- GlobalVariable *StringGV = nullptr ;
1481- if (GlobalStringMap.find (Value.str ()) != GlobalStringMap.end ())
1482- return GlobalStringMap.at (Value.str ());
1483-
1484- auto *Ty =
1485- ArrayType::get (Type::getInt8Ty (M.getContext ()), Value.size () + 1 );
1486- StringGV = new GlobalVariable (
1487- M, Ty, true , GlobalValue::InternalLinkage,
1488- ConstantDataArray::getString (M.getContext (), Value), Name, nullptr ,
1489- GlobalValue::NotThreadLocal, AddressSpace);
1490- GlobalStringMap[Value.str ()] = StringGV;
1491-
1492- return StringGV;
1493- }
1494-
14951550 // / Helper function to insert a warning at IRB's current insert point.
14961551 void insertWarningFn (IRBuilder<> &IRB, Value *Origin) {
14971552 if (!Origin)
@@ -1562,7 +1617,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
15621617 if (auto &Loc = ConvertedShadowInst->getDebugLoc ()) {
15631618 llvm::SmallString<128 > Source = Loc->getDirectory ();
15641619 sys::path::append (Source, Loc->getFilename ());
1565- auto *FileNameGV = GetOrCreateGlobalString (
1620+ auto *FileNameGV = getOrCreateGlobalString (
15661621 *M, " __asan_file" , Source, kSpirOffloadConstantAS );
15671622 Args.push_back (
15681623 ConstantExpr::getPointerCast (FileNameGV, ConstASPtrTy));
@@ -1581,7 +1636,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
15811636
15821637 // function name
15831638 auto FuncName = F.getName ();
1584- auto *FuncNameGV = GetOrCreateGlobalString (
1639+ auto *FuncNameGV = getOrCreateGlobalString (
15851640 *M, " __asan_func" , demangle (FuncName), kSpirOffloadConstantAS );
15861641 Args.push_back (
15871642 ConstantExpr::getPointerCast (FuncNameGV, ConstASPtrTy));
0 commit comments