Auto merge of #137271 - nikic:gep-nuw-2, r=scottmcm

Emit getelementptr inbounds nuw for pointer::add()

Lower pointer::add (via intrinsic::offset with unsigned offset) to getelementptr inbounds nuw on LLVM versions that support it. This lets LLVM make use of the pre-condition that the offset addition does not wrap in an unsigned sense. Together with inbounds, this also implies that the offset is non-negative.

Fixes https://github.com/rust-lang/rust/issues/137217.
This commit is contained in:
bors 2025-02-24 03:06:16 +00:00
commit e0be1a0262
10 changed files with 93 additions and 29 deletions

View file

@ -1767,6 +1767,24 @@ extern "C" LLVMValueRef LLVMRustBuildMaxNum(LLVMBuilderRef B, LLVMValueRef LHS,
return wrap(unwrap(B)->CreateMaxNum(unwrap(LHS), unwrap(RHS)));
}
#if LLVM_VERSION_LT(19, 0)
enum {
LLVMGEPFlagInBounds = (1 << 0),
LLVMGEPFlagNUSW = (1 << 1),
LLVMGEPFlagNUW = (1 << 2),
};
extern "C" LLVMValueRef
LLVMBuildGEPWithNoWrapFlags(LLVMBuilderRef B, LLVMTypeRef Ty,
LLVMValueRef Pointer, LLVMValueRef *Indices,
unsigned NumIndices, const char *Name,
unsigned NoWrapFlags) {
if (NoWrapFlags & LLVMGEPFlagInBounds)
return LLVMBuildInBoundsGEP2(B, Ty, Pointer, Indices, NumIndices, Name);
else
return LLVMBuildGEP2(B, Ty, Pointer, Indices, NumIndices, Name);
}
#endif
// Transfers ownership of DiagnosticHandler unique_ptr to the caller.
extern "C" DiagnosticHandler *
LLVMRustContextGetDiagnosticHandler(LLVMContextRef C) {