@@ -767,50 +767,110 @@ Constant *getOrCreateGlobalString(Module &M, StringRef Name, StringRef Value,
767
767
});
768
768
}
769
769
770
- static void extendSpirKernelArgs (Module &M) {
771
- SmallVector<Constant *, 8 > SpirKernelsMetadata;
770
+ static bool isUnsupportedDeviceGlobal (const GlobalVariable *G) {
771
+ // Skip instrumenting on "__MsanKernelMetadata" etc.
772
+ if (G->getName ().starts_with (" __Msan" ))
773
+ return true ;
774
+ if (G->getName ().starts_with (" __spirv_BuiltIn" ))
775
+ return true ;
776
+ if (G->getName ().starts_with (" __usid_str" ))
777
+ return true ;
778
+ if (G->getAddressSpace () == kSpirOffloadLocalAS ||
779
+ G->getAddressSpace () == kSpirOffloadConstantAS )
780
+ return true ;
781
+ return false ;
782
+ }
783
+
784
+ static void instrumentSPIRModule (Module &M) {
772
785
773
786
const auto &DL = M.getDataLayout ();
774
787
Type *IntptrTy = DL.getIntPtrType (M.getContext ());
775
788
776
- // SpirKernelsMetadata only saves fixed kernels, and is described by
777
- // following structure:
778
- // uptr unmangled_kernel_name
779
- // uptr unmangled_kernel_name_size
780
- StructType *StructTy = StructType::get (IntptrTy, IntptrTy);
781
- for (Function &F : M) {
782
- if (F.getCallingConv () != CallingConv::SPIR_KERNEL)
783
- continue ;
789
+ // Instrument __MsanKernelMetadata, which records information of sanitized
790
+ // kernel
791
+ {
792
+ SmallVector<Constant *, 8 > SpirKernelsMetadata;
793
+
794
+ // SpirKernelsMetadata only saves fixed kernels, and is described by
795
+ // following structure:
796
+ // uptr unmangled_kernel_name
797
+ // uptr unmangled_kernel_name_size
798
+ StructType *StructTy = StructType::get (IntptrTy, IntptrTy);
799
+ for (Function &F : M) {
800
+ if (F.getCallingConv () != CallingConv::SPIR_KERNEL)
801
+ continue ;
784
802
785
- if (!F.hasFnAttribute (Attribute::SanitizeMemory) ||
786
- F.hasFnAttribute (Attribute::DisableSanitizerInstrumentation))
787
- continue ;
803
+ if (!F.hasFnAttribute (Attribute::SanitizeMemory) ||
804
+ F.hasFnAttribute (Attribute::DisableSanitizerInstrumentation))
805
+ continue ;
788
806
789
- auto KernelName = F.getName ();
790
- auto *KernelNameGV = getOrCreateGlobalString (M, " __msan_kernel" , KernelName,
791
- kSpirOffloadConstantAS );
792
- SpirKernelsMetadata.emplace_back (ConstantStruct::get (
793
- StructTy, ConstantExpr::getPointerCast (KernelNameGV, IntptrTy),
794
- ConstantInt::get (IntptrTy, KernelName.size ())));
795
- }
796
-
797
- // Create global variable to record spirv kernels' information
798
- ArrayType *ArrayTy = ArrayType::get (StructTy, SpirKernelsMetadata.size ());
799
- Constant *MetadataInitializer =
800
- ConstantArray::get (ArrayTy, SpirKernelsMetadata);
801
- GlobalVariable *MsanSpirKernelMetadata = new GlobalVariable (
802
- M, MetadataInitializer->getType (), false , GlobalValue::AppendingLinkage,
803
- MetadataInitializer, " __MsanKernelMetadata" , nullptr ,
804
- GlobalValue::NotThreadLocal, 1 );
805
- MsanSpirKernelMetadata->setUnnamedAddr (GlobalValue::UnnamedAddr::Local);
806
- // Add device global attributes
807
- MsanSpirKernelMetadata->addAttribute (
808
- " sycl-device-global-size" , std::to_string (DL.getTypeAllocSize (ArrayTy)));
809
- MsanSpirKernelMetadata->addAttribute (" sycl-device-image-scope" );
810
- MsanSpirKernelMetadata->addAttribute (" sycl-host-access" , " 0" ); // read only
811
- MsanSpirKernelMetadata->addAttribute (" sycl-unique-id" ,
812
- " _Z20__MsanKernelMetadata" );
813
- MsanSpirKernelMetadata->setDSOLocal (true );
807
+ auto KernelName = F.getName ();
808
+ auto *KernelNameGV = getOrCreateGlobalString (
809
+ M, " __msan_kernel" , KernelName, kSpirOffloadConstantAS );
810
+ SpirKernelsMetadata.emplace_back (ConstantStruct::get (
811
+ StructTy, ConstantExpr::getPointerCast (KernelNameGV, IntptrTy),
812
+ ConstantInt::get (IntptrTy, KernelName.size ())));
813
+ }
814
+
815
+ // Create global variable to record spirv kernels' information
816
+ ArrayType *ArrayTy = ArrayType::get (StructTy, SpirKernelsMetadata.size ());
817
+ Constant *MetadataInitializer =
818
+ ConstantArray::get (ArrayTy, SpirKernelsMetadata);
819
+ GlobalVariable *MsanSpirKernelMetadata = new GlobalVariable (
820
+ M, MetadataInitializer->getType (), false , GlobalValue::AppendingLinkage,
821
+ MetadataInitializer, " __MsanKernelMetadata" , nullptr ,
822
+ GlobalValue::NotThreadLocal, 1 );
823
+ MsanSpirKernelMetadata->setUnnamedAddr (GlobalValue::UnnamedAddr::Local);
824
+ // Add device global attributes
825
+ MsanSpirKernelMetadata->addAttribute (
826
+ " sycl-device-global-size" ,
827
+ std::to_string (DL.getTypeAllocSize (ArrayTy)));
828
+ MsanSpirKernelMetadata->addAttribute (" sycl-device-image-scope" );
829
+ MsanSpirKernelMetadata->addAttribute (" sycl-host-access" ,
830
+ " 0" ); // read only
831
+ MsanSpirKernelMetadata->addAttribute (" sycl-unique-id" ,
832
+ " _Z20__MsanKernelMetadata" );
833
+ MsanSpirKernelMetadata->setDSOLocal (true );
834
+ }
835
+
836
+ // Handle global variables:
837
+ // - Skip sanitizing unsupported variables
838
+ // - Instrument __MsanDeviceGlobalMetadata for device globals
839
+ do {
840
+ SmallVector<Constant *, 8 > DeviceGlobalMetadata;
841
+
842
+ // Device global meta data is described by a structure
843
+ // size_t device_global_size
844
+ // size_t beginning address of the device global
845
+ StructType *StructTy = StructType::get (IntptrTy, IntptrTy);
846
+
847
+ for (auto &G : M.globals ()) {
848
+ if (isUnsupportedDeviceGlobal (&G)) {
849
+ for (auto *User : G.users ())
850
+ if (auto *Inst = dyn_cast<Instruction>(User))
851
+ Inst->setNoSanitizeMetadata ();
852
+ continue ;
853
+ }
854
+
855
+ DeviceGlobalMetadata.push_back (ConstantStruct::get (
856
+ StructTy,
857
+ ConstantInt::get (IntptrTy, DL.getTypeAllocSize (G.getValueType ())),
858
+ ConstantExpr::getPointerCast (&G, IntptrTy)));
859
+ }
860
+
861
+ if (DeviceGlobalMetadata.empty ())
862
+ break ;
863
+
864
+ // Create meta data global to record device globals' information
865
+ ArrayType *ArrayTy = ArrayType::get (StructTy, DeviceGlobalMetadata.size ());
866
+ Constant *MetadataInitializer =
867
+ ConstantArray::get (ArrayTy, DeviceGlobalMetadata);
868
+ GlobalVariable *MsanDeviceGlobalMetadata = new GlobalVariable (
869
+ M, MetadataInitializer->getType (), false , GlobalValue::AppendingLinkage,
870
+ MetadataInitializer, " __MsanDeviceGlobalMetadata" , nullptr ,
871
+ GlobalValue::NotThreadLocal, 1 );
872
+ MsanDeviceGlobalMetadata->setUnnamedAddr (GlobalValue::UnnamedAddr::Local);
873
+ } while (false );
814
874
}
815
875
816
876
PreservedAnalyses MemorySanitizerPass::run (Module &M,
@@ -827,7 +887,7 @@ PreservedAnalyses MemorySanitizerPass::run(Module &M,
827
887
}
828
888
829
889
if (TargetTriple.isSPIROrSPIRV ()) {
830
- extendSpirKernelArgs (M);
890
+ instrumentSPIRModule (M);
831
891
Modified = true ;
832
892
}
833
893
0 commit comments