Skip to content

[InstrProf] Change step from 64-bit to pointer-sized int #83239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/IntrinsicInst.h
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,7 @@ class InstrProfIncrementInst : public InstrProfCntrInstBase {
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
}
Value *getStep() const;
Value *getAtomicStep() const;
};

/// This represents the llvm.instrprof.increment.step intrinsic.
Expand Down
17 changes: 17 additions & 0 deletions llvm/lib/IR/IntrinsicInst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,26 @@ Value *InstrProfIncrementInst::getStep() const {
}
const Module *M = getModule();
LLVMContext &Context = M->getContext();
const auto &DL = M->getDataLayout();
Type *LargestLegalIntTy = DL.getLargestLegalIntType(Context);

if (LargestLegalIntTy) {
return ConstantInt::get(LargestLegalIntTy, 1);
}

return ConstantInt::get(Type::getInt64Ty(Context), 1);
}

Value *InstrProfIncrementInst::getAtomicStep() const {
if (InstrProfIncrementInstStep::classof(this)) {
return const_cast<Value *>(getArgOperand(4));
}
const Module *M = getModule();
LLVMContext &Context = M->getContext();
const auto &DL = M->getDataLayout();
return ConstantInt::get(DL.getIntPtrType(Context), 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a 64-bit type, very few counters will overflow. With a pointer-width type, more counters will overflow, especially on 16-bit targets, but even on 32-bit targets. I speculate this is why 64-bit integers are used currently. How should we address this concern?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I pushed some changes, creating additional getStep function for atomics. This way, we can use a larger type.

Also, I've taken the liberty of changing the existing code to use the largest legal int available instead of the 64-bit int.

}

std::optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
unsigned NumOperands = arg_size();
Metadata *MD = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ void InstrLowerer::lowerIncrement(InstrProfIncrementInst *Inc) {
IRBuilder<> Builder(Inc);
if (Options.Atomic || AtomicCounterUpdateAll ||
(Inc->getIndex()->isZeroValue() && AtomicFirstCounter)) {
Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(),
Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getAtomicStep(),
MaybeAlign(), AtomicOrdering::Monotonic);
} else {
Value *IncStep = Inc->getStep();
Expand Down