Skip to content

Commit b7407d3

Browse files
[InstrProf] Change step from 64-bit to data layout dependent size
Fixed 64-bit step can lead to creating atomic instructions unsupported by the target architecture (see rust-lang/rust#112313). When using atomics, type of the step is a pointer-sized integer. Otherwise, type of the step is of a largest legal integer type.
1 parent 6008cd4 commit b7407d3

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

llvm/include/llvm/IR/IntrinsicInst.h

+1
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,7 @@ class InstrProfIncrementInst : public InstrProfCntrInstBase {
14721472
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
14731473
}
14741474
Value *getStep() const;
1475+
Value *getAtomicStep() const;
14751476
};
14761477

14771478
/// This represents the llvm.instrprof.increment.step intrinsic.

llvm/lib/IR/IntrinsicInst.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,26 @@ Value *InstrProfIncrementInst::getStep() const {
288288
}
289289
const Module *M = getModule();
290290
LLVMContext &Context = M->getContext();
291+
const auto &DL = M->getDataLayout();
292+
Type* LargestLegalIntTy = DL.getLargestLegalIntType(Context);
293+
294+
if (LargestLegalIntTy) {
295+
return ConstantInt::get(LargestLegalIntTy, 1);
296+
}
297+
291298
return ConstantInt::get(Type::getInt64Ty(Context), 1);
292299
}
293300

301+
Value *InstrProfIncrementInst::getAtomicStep() const {
302+
if (InstrProfIncrementInstStep::classof(this)) {
303+
return const_cast<Value *>(getArgOperand(4));
304+
}
305+
const Module *M = getModule();
306+
LLVMContext &Context = M->getContext();
307+
const auto &DL = M->getDataLayout();
308+
return ConstantInt::get(DL.getIntPtrType(Context), 1);
309+
}
310+
294311
std::optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
295312
unsigned NumOperands = arg_size();
296313
Metadata *MD = nullptr;

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) {
935935
IRBuilder<> Builder(Inc);
936936
if (Options.Atomic || AtomicCounterUpdateAll ||
937937
(Inc->getIndex()->isZeroValue() && AtomicFirstCounter)) {
938-
Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
938+
Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getAtomicStep(),
939939
MaybeAlign(), AtomicOrdering::Monotonic);
940940
} else {
941941
Value *IncStep = Inc->getStep();

0 commit comments

Comments
 (0)