Skip to content

Commit 5e9d8a7

Browse files
committed
Switch to the LLVMBuildGEPWithNoWrapFlags API
This API allows us to set the nuw flag as well.
1 parent 3b022d8 commit 5e9d8a7

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::abi::FnAbiLlvmExt;
3232
use crate::attributes;
3333
use crate::common::Funclet;
3434
use crate::context::{CodegenCx, SimpleCx};
35-
use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, Metadata, True};
35+
use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, GEPNoWrapFlags, Metadata, True};
3636
use crate::type_::Type;
3737
use crate::type_of::LayoutLlvmExt;
3838
use crate::value::Value;
@@ -908,13 +908,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
908908

909909
fn gep(&mut self, ty: &'ll Type, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
910910
unsafe {
911-
llvm::LLVMBuildGEP2(
911+
llvm::LLVMBuildGEPWithNoWrapFlags(
912912
self.llbuilder,
913913
ty,
914914
ptr,
915915
indices.as_ptr(),
916916
indices.len() as c_uint,
917917
UNNAMED,
918+
GEPNoWrapFlags::default(),
918919
)
919920
}
920921
}
@@ -926,13 +927,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
926927
indices: &[&'ll Value],
927928
) -> &'ll Value {
928929
unsafe {
929-
llvm::LLVMBuildInBoundsGEP2(
930+
llvm::LLVMBuildGEPWithNoWrapFlags(
930931
self.llbuilder,
931932
ty,
932933
ptr,
933934
indices.as_ptr(),
934935
indices.len() as c_uint,
935936
UNNAMED,
937+
GEPNoWrapFlags::InBounds,
936938
)
937939
}
938940
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,17 @@ bitflags! {
954954
}
955955
}
956956

957+
// These values **must** match with LLVMGEPNoWrapFlags
958+
bitflags! {
959+
#[repr(transparent)]
960+
#[derive(Default)]
961+
pub struct GEPNoWrapFlags : c_uint {
962+
const InBounds = 1 << 0;
963+
const NUSW = 1 << 1;
964+
const NUW = 1 << 2;
965+
}
966+
}
967+
957968
unsafe extern "C" {
958969
pub type ModuleBuffer;
959970
}
@@ -1454,21 +1465,14 @@ unsafe extern "C" {
14541465

14551466
pub(crate) fn LLVMBuildStore<'a>(B: &Builder<'a>, Val: &'a Value, Ptr: &'a Value) -> &'a Value;
14561467

1457-
pub(crate) fn LLVMBuildGEP2<'a>(
1458-
B: &Builder<'a>,
1459-
Ty: &'a Type,
1460-
Pointer: &'a Value,
1461-
Indices: *const &'a Value,
1462-
NumIndices: c_uint,
1463-
Name: *const c_char,
1464-
) -> &'a Value;
1465-
pub(crate) fn LLVMBuildInBoundsGEP2<'a>(
1468+
pub(crate) fn LLVMBuildGEPWithNoWrapFlags<'a>(
14661469
B: &Builder<'a>,
14671470
Ty: &'a Type,
14681471
Pointer: &'a Value,
14691472
Indices: *const &'a Value,
14701473
NumIndices: c_uint,
14711474
Name: *const c_char,
1475+
Flags: GEPNoWrapFlags,
14721476
) -> &'a Value;
14731477

14741478
// Casts

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,24 @@ extern "C" LLVMValueRef LLVMRustBuildMaxNum(LLVMBuilderRef B, LLVMValueRef LHS,
17671767
return wrap(unwrap(B)->CreateMaxNum(unwrap(LHS), unwrap(RHS)));
17681768
}
17691769

1770+
#if LLVM_VERSION_LT(19, 0)
1771+
enum {
1772+
LLVMGEPFlagInBounds = (1 << 0),
1773+
LLVMGEPFlagNUSW = (1 << 1),
1774+
LLVMGEPFlagNUW = (1 << 2),
1775+
};
1776+
extern "C" LLVMValueRef
1777+
LLVMBuildGEPWithNoWrapFlags(LLVMBuilderRef B, LLVMTypeRef Ty,
1778+
LLVMValueRef Pointer, LLVMValueRef *Indices,
1779+
unsigned NumIndices, const char *Name,
1780+
unsigned NoWrapFlags) {
1781+
if (NoWrapFlags & LLVMGEPFlagInBounds)
1782+
return LLVMBuildInBoundsGEP2(B, Ty, Pointer, Indices, NumIndices, Name);
1783+
else
1784+
return LLVMBuildGEP2(B, Ty, Pointer, Indices, NumIndices, Name);
1785+
}
1786+
#endif
1787+
17701788
// Transfers ownership of DiagnosticHandler unique_ptr to the caller.
17711789
extern "C" DiagnosticHandler *
17721790
LLVMRustContextGetDiagnosticHandler(LLVMContextRef C) {

0 commit comments

Comments
 (0)