[llvm-branch-commits] [llvm] [AMDGPU] Fold provably out-of-bounds raw buffer loads/stores (PR #227402)
Matt Arsenault via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Oct 3 02:59:44 PDT 2026
================
@@ -1175,6 +1175,80 @@ static Instruction *foldConstantIntoDotAccumulator(IntrinsicInst &II,
return IC.replaceOperand(II, AccIdx, NewAcc);
}
+// Check raw buffer access is OOB or not. For partial OOB access, just bail out.
+static bool isRawBufferAccessDefinitelyOOB(const GCNSubtarget *ST, Value *Rsrc,
+ Value *OffsetV, Value *SOffsetV,
+ Value *AuxV, uint64_t Payload,
+ const DataLayout &DL) {
+ using OOBModel = GCNSubtarget::AMDGPUBufferOOBModel;
+ std::optional<OOBModel> Model = ST->getBufferOOBModel();
+ std::optional<unsigned> NumRecordsWidth =
+ ST->getBufferResourceNumRecordsWidth();
+ if (!Model || !NumRecordsWidth)
+ return false;
+
+ auto *AuxC = dyn_cast<ConstantInt>(AuxV);
+ if (!AuxC)
+ return false;
+
+ uint64_t Offset = computeKnownBits(OffsetV, DL).getMinValue().getZExtValue();
+ uint64_t SOffset =
+ computeKnownBits(SOffsetV, DL).getMinValue().getZExtValue();
+
+ uint64_t Aux = AuxC->getZExtValue();
+ if (Aux & AMDGPU::CPol::VOLATILE)
+ return false;
+
+ // The resource descriptor must also be a compile-time constant.
+ const APInt *StrideAP, *NumRecAP, *FlagsAP;
+ if (!match(Rsrc, m_Intrinsic<Intrinsic::amdgcn_make_buffer_rsrc>(
+ m_Value(), m_APInt(StrideAP), m_APInt(NumRecAP),
+ m_APInt(FlagsAP))))
+ return false;
+
+ uint64_t StrideRaw = StrideAP->getZExtValue();
+ uint64_t Stride = StrideRaw & 0x3fff;
+ uint64_t Flags = FlagsAP->getZExtValue();
+ uint64_t NumRecordsMask = maskTrailingOnes<uint64_t>(*NumRecordsWidth);
+ uint64_t NumRecords =
+ NumRecAP->zextOrTrunc(64).getZExtValue() & NumRecordsMask;
+
+ switch (*Model) {
+ case OOBModel::Gfx9:
----------------
arsenm wrote:
It would be nicer if these were named according to the behavior instead of the targets with the behavior
https://github.com/llvm/llvm-project/pull/227402
More information about the llvm-branch-commits
mailing list