Skip to content

Commit 530c1a6

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 d4569d4 commit 530c1a6

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
@@ -1506,6 +1506,7 @@ class InstrProfIncrementInst : public InstrProfCntrInstBase {
15061506
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
15071507
}
15081508
Value *getStep() const;
1509+
Value *getAtomicStep() const;
15091510
};
15101511

15111512
/// 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
@@ -958,7 +958,7 @@ void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) {
958958
IRBuilder<> Builder(Inc);
959959
if (Options.Atomic || AtomicCounterUpdateAll ||
960960
(Inc->getIndex()->isZeroValue() && AtomicFirstCounter)) {
961-
Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
961+
Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getAtomicStep(),
962962
MaybeAlign(), AtomicOrdering::Monotonic);
963963
} else {
964964
Value *IncStep = Inc->getStep();

0 commit comments

Comments
 (0)