================
@@ -534,6 +582,171 @@ bool
ConcurrencySanitizer::instrumentMemIntrinsic(IntrinsicInst *II,
return true;
}
+// Builds a loop that spins until a timed deadline is complete.
+static void insertDeadlineLoop(Instruction *InsertBefore, Value *End,
+ Intrinsic::ID Clock, Intrinsic::ID Wait,
+ Value *WaitArg) {
+ BasicBlock *Pre = InsertBefore->getParent();
+ BasicBlock *Check =
+ Pre->splitBasicBlock(InsertBefore->getIterator(), "csan.delay");
+ BasicBlock *Done =
+ Check->splitBasicBlock(InsertBefore->getIterator(), "csan.delay.done");
+ BasicBlock *Body = BasicBlock::Create(Pre->getContext(), "csan.delay.body",
+ Pre->getParent(), Done);
+
+ Instruction *ToDone = Check->getTerminator();
+ InstrumentationIRBuilder C(ToDone);
+ Value *Now = C.CreateIntrinsic(Clock, {});
+ C.CreateCondBr(C.CreateICmpULT(Now, End, "csan.delaying"), Body, Done);
+ ToDone->eraseFromParent();
+
+ IRBuilder<> B(Body);
+ InstrumentationIRBuilder::ensureDebugInfo(B, *Pre->getParent());
+ if (WaitArg)
+ B.CreateIntrinsic(Wait, {WaitArg});
+ else
+ B.CreateIntrinsic(Wait, {});
+ B.CreateBr(Check);
+}
+
+void ConcurrencySanitizer::insertInlineDelay(Instruction *InsertBefore) {
+ Module *M = InsertBefore->getModule();
+ InstrumentationIRBuilder IRB(InsertBefore);
+ if (M->getTargetTriple().isAMDGPU()) {
+ // 1000 ticks is 10us at the 100MHz rate the device sampler assumes.
+ // s_sleep(15) is the yield that sampler uses between polls.
+ constexpr uint64_t AmdgpuDelayTicks = 1000;
+ Value *End =
+ IRB.CreateAdd(IRB.CreateIntrinsic(Intrinsic::readsteadycounter, {}),
+ IRB.getInt64(AmdgpuDelayTicks), "csan.delay.end");
+ IRB.CreateIntrinsic(Intrinsic::amdgcn_s_sleep, {IRB.getInt32(2)});
+ insertDeadlineLoop(InsertBefore, End, Intrinsic::readsteadycounter,
+ Intrinsic::amdgcn_s_sleep, IRB.getInt32(15));
+ return;
+ }
+ if (M->getTargetTriple().isNVPTX()) {
+ // The operand is nanoseconds. 10us is the device sampler's upper delay.
+ constexpr unsigned NvptxDelayNs = 10000;
+ IRB.CreateIntrinsic(Intrinsic::nvvm_nanosleep,
+ {IRB.getInt32(NvptxDelayNs)});
+ return;
+ }
+
+ // Default udelay of the host sampler.
+ constexpr unsigned HostDelayUs = 80;
+ FunctionCallee USleep;
+ if (isLibFuncEmittable(M, LibInfo, LibFunc_usleep)) {
+ USleep = getOrInsertLibFunc(M, *LibInfo, LibFunc_usleep, IRB.getInt32Ty(),
+ IRB.getInt32Ty());
+ inferNonMandatoryLibFuncAttrs(M, LibInfo->getName(LibFunc_usleep),
+ *LibInfo);
+ } else {
+ USleep =
+ M->getOrInsertFunction("usleep", IRB.getInt32Ty(), IRB.getInt32Ty());
+ }
+ CallInst *CI = IRB.CreateCall(USleep, {IRB.getInt32(HostDelayUs)});
+ if (auto *F = dyn_cast<Function>(USleep.getCallee()))
+ CI->setCallingConv(F->getCallingConv());
+}
+
+// A memcpy has no value type. Read at most 8 bytes, or one byte when the
+// length is not a constant.
+static Type *rangeSampleTy(LLVMContext &Ctx, Value *ByteSize) {
+ uint64_t Bytes = 1;
+ if (auto *C = dyn_cast<ConstantInt>(ByteSize)) {
+ Bytes = C->getZExtValue();
+ if (Bytes > 8)
+ Bytes = 8;
+ }
+ return Type::getIntNTy(Ctx, Bytes * 8);
+}
+
+bool ConcurrencySanitizer::insertTypedValueCheck(Instruction *I, Value *Addr,
+ Type *AccessTy,
+ const DataLayout &DL,
+ bool IsWrite) {
+ if (Addr->isSwiftError() || !AccessTy->isSized())
+ return false;
+ TypeSize StoreSize = DL.getTypeStoreSize(AccessTy);
+ if (StoreSize.isZero())
+ return false;
+
+ // icmp has no aggregate form. Load the stored bytes as an integer.
+ Type *LoadTy = AccessTy;
+ if (!AccessTy->isIntOrIntVectorTy() && !AccessTy->isPtrOrPtrVectorTy() &&
+ !AccessTy->isFPOrFPVectorTy()) {
+ TypeSize Bits = DL.getTypeStoreSizeInBits(AccessTy);
+ if (!Bits.isFixed())
+ return false;
+ LoadTy = Type::getIntNTy(AccessTy->getContext(), Bits.getFixedValue());
+ }
+
+ InstrumentationIRBuilder IRB(I);
+ Value *ByteSize = IRB.CreateTypeSize(IntptrTy, StoreSize);
+ return emitValueCheck(I, Addr, LoadTy, ByteSize, getLoadStoreAlignment(I),
+ IsWrite);
+}
+
+bool ConcurrencySanitizer::insertRangeValueCheck(Instruction *I, Value *Addr,
+ Value *ByteSize,
+ bool IsWrite) {
+ if (auto *C = dyn_cast<ConstantInt>(ByteSize)) {
+ if (C->isZero())
+ return false;
+ } else {
+ // A dynamic length may be zero.
+ InstrumentationIRBuilder IRB(I);
+ I = SplitBlockAndInsertIfThen(IRB.CreateIsNotNull(ByteSize), I,
+ /*Unreachable=*/false);
+ }
+ return emitValueCheck(I, Addr, rangeSampleTy(I->getContext(), ByteSize),
+ ByteSize, Align(1), IsWrite);
+}
+
+bool ConcurrencySanitizer::emitValueCheck(Instruction *I, Value *Addr,
+ Type *LoadTy, Value *ByteSize,
+ Align Alignment, bool IsWrite) {
+ const DataLayout &DL = I->getModule()->getDataLayout();
+ MDNode *NoSanitize = MDNode::get(I->getContext(), {});
+ InstrumentationIRBuilder IRB(I);
+ LoadInst *Old = IRB.CreateAlignedLoad(LoadTy, Addr, Alignment,
+ /*isVolatile=*/true, "csan.old");
+ Old->setMetadata(LLVMContext::MD_nosanitize, NoSanitize);
+
+ insertInlineDelay(I);
+
+ InstrumentationIRBuilder After(I);
+ LoadInst *New = After.CreateAlignedLoad(LoadTy, Addr, Alignment,
+ /*isVolatile=*/true, "csan.new");
+ New->setMetadata(LLVMContext::MD_nosanitize, NoSanitize);
+
+ // Compare the raw bits. A scalable vector has no fixed integer width.
+ TypeSize Bits = DL.getTypeSizeInBits(LoadTy);
+ Type *BitTy;
+ if (Bits.isScalable())
+ BitTy = LoadTy->isPtrOrPtrVectorTy()
+ ? DL.getIntPtrType(LoadTy)
+ : VectorType::getInteger(cast<VectorType>(LoadTy));
+ else
+ BitTy = After.getIntNTy(Bits.getFixedValue());
+ Value *Changed = After.CreateICmpNE(
+ After.CreateBitPreservingCastChain(DL, Old, BitTy),
----------------
yxsamliu wrote:
Could we build the two casts in separate statements? Windows emits them in the
opposite order, which breaks `CHECK-NEXT`.
https://github.com/llvm/llvm-project/pull/244727
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits