@@ -813,6 +813,8 @@ class MemorySanitizerOnSpirv {
813813 Constant *getOrCreateGlobalString (StringRef Name, StringRef Value,
814814 unsigned AddressSpace);
815815
816+ static bool isSupportedBuiltIn (StringRef Name);
817+
816818 operator bool () const { return IsSPIRV; }
817819
818820private:
@@ -823,7 +825,6 @@ class MemorySanitizerOnSpirv {
823825 void instrumentKernelsMetadata (int TrackOrigins);
824826 void instrumentPrivateArguments (Function &F, Instruction *FnPrologueEnd);
825827 void instrumentPrivateBase (Function &F);
826-
827828 void initializeRetVecMap (Function *F);
828829 void initializeKernelCallerMap (Function *F);
829830
@@ -856,6 +857,7 @@ class MemorySanitizerOnSpirv {
856857 FunctionCallee MsanUnpoisonShadowDynamicLocalFunc;
857858 FunctionCallee MsanBarrierFunc;
858859 FunctionCallee MsanUnpoisonStackFunc;
860+ FunctionCallee MsanUnpoisonShadowFunc;
859861 FunctionCallee MsanSetPrivateBaseFunc;
860862 FunctionCallee MsanUnpoisonStridedCopyFunc;
861863};
@@ -949,6 +951,14 @@ void MemorySanitizerOnSpirv::initializeCallbacks() {
949951 MsanUnpoisonStackFunc = M.getOrInsertFunction (
950952 " __msan_unpoison_stack" , IRB.getVoidTy (), PtrTy, IntptrTy);
951953
954+ // __msan_unpoison_(
955+ // uptr ptr,
956+ // uint32_t as,
957+ // size_t size
958+ // )
959+ MsanUnpoisonShadowFunc = M.getOrInsertFunction (
960+ " __msan_unpoison_shadow" , IRB.getVoidTy (), IntptrTy, Int32Ty, IntptrTy);
961+
952962 // __msan_set_private_base(
953963 // as(0) void * ptr
954964 // )
@@ -987,9 +997,16 @@ void MemorySanitizerOnSpirv::instrumentGlobalVariables() {
987997 G.setName (" nameless_global" );
988998
989999 if (isUnsupportedDeviceGlobal (&G)) {
990- for (auto *User : G.users ())
991- if (auto *Inst = dyn_cast<Instruction>(User))
992- Inst->setNoSanitizeMetadata ();
1000+ for (auto *User : G.users ()) {
1001+ if (!isa<Instruction>(User))
1002+ continue ;
1003+ if (auto *CI = dyn_cast<CallInst>(User)) {
1004+ Function *Callee = CI->getCalledFunction ();
1005+ if (Callee && isSupportedBuiltIn (Callee->getName ()))
1006+ continue ;
1007+ }
1008+ cast<Instruction>(User)->setNoSanitizeMetadata ();
1009+ }
9931010 continue ;
9941011 }
9951012
@@ -1150,6 +1167,10 @@ void MemorySanitizerOnSpirv::instrumentPrivateBase(Function &F) {
11501167 IRB.CreateCall (MsanSetPrivateBaseFunc, {PrivateBase});
11511168}
11521169
1170+ bool MemorySanitizerOnSpirv::isSupportedBuiltIn (StringRef Name) {
1171+ return Name.contains (" __sycl_getComposite2020SpecConstantValue" );
1172+ }
1173+
11531174void MemorySanitizerOnSpirv::instrumentPrivateArguments (
11541175 Function &F, Instruction *FnPrologueEnd) {
11551176 if (!ClSpirOffloadPrivates)
@@ -6994,6 +7015,25 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
69947015 IRB.CreatePointerCast (Src, MS.Spirv .IntptrTy ),
69957016 IRB.getInt32 (Src->getType ()->getPointerAddressSpace ()),
69967017 IRB.getInt32 (ElementSize), NumElements, Stride});
7018+ } else if (FuncName.contains (
7019+ " __sycl_getComposite2020SpecConstantValue" )) {
7020+ // clang-format off
7021+ // Handle builtin functions like "_Z40__sycl_getComposite2020SpecConstantValue"
7022+ // Structs which are larger than 64b will be returned via sret arguments
7023+ // and will be initialized inside the function. So we need to unpoison
7024+ // the sret arguments.
7025+ // clang-format on
7026+ if (Func->hasStructRetAttr ()) {
7027+ Type *SCTy = Func->getParamStructRetType (0 );
7028+ unsigned Size = Func->getDataLayout ().getTypeStoreSize (SCTy);
7029+ auto *Addr = CB.getArgOperand (0 );
7030+ IRB.CreateCall (
7031+ MS.Spirv .MsanUnpoisonShadowFunc ,
7032+ {IRB.CreatePointerCast (Addr, MS.Spirv .IntptrTy ),
7033+ ConstantInt::get (MS.Spirv .Int32Ty ,
7034+ Addr->getType ()->getPointerAddressSpace ()),
7035+ ConstantInt::get (MS.Spirv .IntptrTy , Size)});
7036+ }
69977037 }
69987038 }
69997039 }
0 commit comments