@@ -928,11 +928,21 @@ void MemorySanitizer::createKernelApi(Module &M, const TargetLibraryInfo &TLI) {
928928}
929929
930930static Constant *getOrInsertGlobal (Module &M, StringRef Name, Type *Ty) {
931- return M.getOrInsertGlobal (Name, Ty, [&] {
932- return new GlobalVariable (M, Ty, false , GlobalVariable::ExternalLinkage,
933- nullptr , Name, nullptr ,
934- GlobalVariable::InitialExecTLSModel);
935- });
931+ // FIXME: spirv target doesn't support TLS, need to handle it later.
932+ if (Triple (M.getTargetTriple ()).isSPIROrSPIRV ()) {
933+ return M.getOrInsertGlobal (Name, Ty, [&] {
934+ return new GlobalVariable (M, Ty, false , GlobalVariable::ExternalLinkage,
935+ Constant::getNullValue (Ty), Name, nullptr ,
936+ GlobalVariable::NotThreadLocal,
937+ kSpirOffloadGlobalAS );
938+ });
939+ } else {
940+ return M.getOrInsertGlobal (Name, Ty, [&] {
941+ return new GlobalVariable (M, Ty, false , GlobalVariable::ExternalLinkage,
942+ nullptr , Name, nullptr ,
943+ GlobalVariable::InitialExecTLSModel);
944+ });
945+ }
936946}
937947
938948// / Insert declarations for userspace-specific functions and globals.
@@ -1256,6 +1266,11 @@ static unsigned TypeSizeToSizeIndex(TypeSize TS) {
12561266}
12571267
12581268static bool isUnsupportedSPIRAccess (const Value *Addr, Instruction *I) {
1269+ if (isa<Instruction>(Addr) &&
1270+ cast<Instruction>(Addr)->getMetadata (LLVMContext::MD_nosanitize)) {
1271+ return true ;
1272+ }
1273+
12591274 // Skip SPIR-V built-in varibles
12601275 auto *OrigValue = Addr->stripInBoundsOffsets ();
12611276 assert (OrigValue != nullptr );
@@ -1266,8 +1281,9 @@ static bool isUnsupportedSPIRAccess(const Value *Addr, Instruction *I) {
12661281 switch (PtrTy->getPointerAddressSpace ()) {
12671282 case kSpirOffloadPrivateAS :
12681283 case kSpirOffloadLocalAS :
1269- case kSpirOffloadGenericAS :
12701284 return true ;
1285+ case kSpirOffloadGenericAS :
1286+ return false ;
12711287 }
12721288
12731289 return false ;
@@ -1283,8 +1299,12 @@ static void setNoSanitizedMetadataSPIR(Instruction &I) {
12831299 Addr = RMW->getPointerOperand ();
12841300 else if (const auto *XCHG = dyn_cast<AtomicCmpXchgInst>(&I))
12851301 Addr = XCHG->getPointerOperand ();
1302+ else if (const auto *ASC = dyn_cast<AddrSpaceCastInst>(&I))
1303+ Addr = ASC->getPointerOperand ();
12861304 else if (isa<MemCpyInst>(&I))
12871305 I.setNoSanitizeMetadata ();
1306+ else if (isa<AllocaInst>(&I))
1307+ I.setNoSanitizeMetadata ();
12881308 else if (const auto *CI = dyn_cast<CallInst>(&I)) {
12891309 auto *Func = CI->getCalledFunction ();
12901310 if (Func) {
@@ -1308,6 +1328,11 @@ static void setNoSanitizedMetadataSPIR(Instruction &I) {
13081328 Addr = CI->getOperand (OpOffset);
13091329 break ;
13101330 }
1331+ case Intrinsic::lifetime_start:
1332+ case Intrinsic::lifetime_end: {
1333+ I.setNoSanitizeMetadata ();
1334+ break ;
1335+ }
13111336 }
13121337 } else {
13131338 auto FuncName = Func->getName ();
@@ -2243,7 +2268,10 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
22432268 unsigned ArgOffset = 0 ;
22442269 const DataLayout &DL = F->getDataLayout ();
22452270 for (auto &FArg : F->args ()) {
2246- if (!FArg.getType ()->isSized () || FArg.getType ()->isScalableTy ()) {
2271+ // FIXME: Need to find a reasonable way to handle byval arguments for
2272+ // spirv target.
2273+ if (!FArg.getType ()->isSized () || FArg.getType ()->isScalableTy () ||
2274+ (SpirOrSpirv && FArg.hasByValAttr ())) {
22472275 LLVM_DEBUG (dbgs () << (FArg.getType ()->isScalableTy ()
22482276 ? " vscale not fully supported\n "
22492277 : " Arg is not sized\n " ));
@@ -3183,6 +3211,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
31833211
31843212 // Same as memcpy.
31853213 void visitMemSetInst (MemSetInst &I) {
3214+ if (SpirOrSpirv && isa<Instruction>(I.getArgOperand (0 )) &&
3215+ cast<Instruction>(I.getArgOperand (0 ))
3216+ ->getMetadata (LLVMContext::MD_nosanitize))
3217+ return ;
3218+
31863219 IRBuilder<> IRB (&I);
31873220 IRB.CreateCall (
31883221 SpirOrSpirv ? MS.MemsetOffloadFn [cast<PointerType>(
@@ -4763,7 +4796,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
47634796 bool NoUndef = CB.paramHasAttr (i, Attribute::NoUndef);
47644797 bool EagerCheck = MayCheckCall && !ByVal && NoUndef;
47654798
4766- if (EagerCheck) {
4799+ // Always do eager check for spirv target
4800+ if (EagerCheck || SpirOrSpirv) {
47674801 insertShadowCheck (A, &CB);
47684802 Size = DL.getTypeAllocSize (A->getType ());
47694803 } else {
0 commit comments