[llvm] [Attributor] Change allocation size and load/store offsets using AAPointerInfo for Alloca instructions (PR #72029)
Vidush Singhal via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 22 19:19:10 PDT 2024
================
@@ -6285,12 +6343,106 @@ struct AAAllocationInfo : public StateWrapper<BooleanState, AbstractAttribute> {
return AbstractAttribute::isValidIRPositionForInit(A, IRP);
}
+ // A helper function to check if simplified values exists for the current
+ // instruction.
+ // TODO: handle case for a similified value
+ // Right now we don't change the value and give up
+ // on modifying the size and offsets of the allocation
+ // this may be sub-optimal
+ bool checkIfSimplifiedValuesExists(Attributor &A, Instruction *LocalInst) {
+
+ // If there are potential values that replace the accessed instruction, we
+ // should use those instead
+ bool UsedAssumedInformation = false;
+ SmallVector<AA::ValueAndContext> Values;
+ if (A.getAssumedSimplifiedValues(IRPosition::inst(*LocalInst), *this,
+ Values, AA::AnyScope,
+ UsedAssumedInformation))
+
+ for (auto &ValAndContext : Values)
+ // don't modify instruction if any simplified value exists
+ if (ValAndContext.getValue() && ValAndContext.getValue() != LocalInst)
+ return true;
+
+ return false;
+ }
+
+ // A helper function to back track the pointer operand to
+ // a GEP that calculates the pointer operand from the original allocation.
+ // So that we can correct the type of the GEP.
+ // If such a GEP is not found, we return nullptr.
+ Instruction *
+ backTrackPointerOperandToGepFromAllocation(Instruction *PointerOperand,
+ Instruction *Allocation) {
+
+ SmallVector<Instruction *> ReadyList;
+ ReadyList.push_back(PointerOperand);
+ while (!ReadyList.empty()) {
+ Instruction *Back = ReadyList.back();
+ ReadyList.pop_back();
+
+ if (Back == Allocation)
+ continue;
+
+ for (auto *It = Back->op_begin(); It != Back->op_end(); It++) {
+ Instruction *OperandInstruction = dyn_cast<Instruction>(It);
+
+ if (!OperandInstruction)
+ continue;
+
+ // This is the GEP instruction whose type and offsets we can change
+ // without any illegal memory access or poison result.
+ if (Back->getOpcode() == Instruction::GetElementPtr &&
+ OperandInstruction == Allocation)
+ return Back;
+
+ ReadyList.push_back(OperandInstruction);
----------------
vidsinghal wrote:
For a Call
```
%ptrOp = call @foo(%These , %pointers, %may , %come , %from , %the , %allocation)
```
We want to first check if any of the pointers in the call, comes from the allocation, they we may want to backtrack the pointers which may come from the allocation to see is a GEP needs to be changed to ensure it accesses the allocation in bounds by changing its type.
I do check what type of instruction it is and then traverse it.
See from this line: https://github.com/llvm/llvm-project/blob/3bbdf3e089e6117c964fd5dbce32f0d3f0c7e2e5/llvm/lib/Transforms/IPO/AttributorAttributes.cpp#L12806
For a Store and Load, I backtrack the pointer operand.
For a Call, for each arg, i check if that arg accesses the offset in question, if so, i backtrack it otherwise i leave it be.
https://github.com/llvm/llvm-project/pull/72029
More information about the llvm-commits
mailing list