Skip to content

Commit 08d9b3e

Browse files
committed
Add enable-getelementptr-nuw flag
1 parent 3547935 commit 08d9b3e

5 files changed

Lines changed: 20 additions & 7 deletions

File tree

driver/cl_options.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,11 @@ cl::opt<unsigned>
751751
cl::desc("Warn for stack size bigger than the given number"),
752752
cl::value_desc("threshold"));
753753

754+
cl::opt<bool>
755+
enableGetElementPtrNuw("enable-getelementptr-nuw", cl::ZeroOrMore,
756+
cl::desc("enable nuw(no-unsigned-wrap) flag to "
757+
"LLVM's getelementptr insturction"));
758+
754759
#if LDC_LLVM_SUPPORTED_TARGET_SPIRV || LDC_LLVM_SUPPORTED_TARGET_NVPTX
755760
cl::list<std::string>
756761
dcomputeTargets("mdcompute-targets", cl::CommaSeparated,

driver/cl_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ extern cl::opt<std::string> saveOptimizationRecord;
140140

141141
extern cl::opt<unsigned> fWarnStackSize;
142142

143+
extern cl::opt<bool> enableGetElementPtrNuw;
144+
143145
#if LDC_LLVM_SUPPORTED_TARGET_SPIRV || LDC_LLVM_SUPPORTED_TARGET_NVPTX
144146
extern cl::list<std::string> dcomputeTargets;
145147
extern cl::opt<std::string> dcomputeFilePrefix;

gen/llvmhelpers.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "dmd/init.h"
1818
#include "dmd/module.h"
1919
#include "dmd/template.h"
20+
#include "driver/cl_options.h"
2021
#include "gen/abi/abi.h"
2122
#include "gen/arrays.h"
2223
#include "gen/classes.h"
@@ -1880,12 +1881,16 @@ DLValue *DtoIndexAggregate(LLValue *src, AggregateDeclaration *ad,
18801881

18811882
LLValue *ptr = src;
18821883
LLType * ty = nullptr;
1884+
#if LDC_LLVM_VER >= 2000
1885+
llvm::GEPNoWrapFlags nw = llvm::GEPNoWrapFlags::inBounds();
1886+
if (opts::enableGetElementPtrNuw)
1887+
nw |= llvm::GEPNoWrapFlags::noUnsignedWrap();
1888+
#endif
18831889
if (!isFieldIdx) {
18841890
// apply byte-wise offset from object start
18851891
ptr = DtoGEP1(getI8Type(), ptr, off
18861892
#if LDC_LLVM_VER >= 2000
1887-
, "", nullptr
1888-
, llvm::GEPNoWrapFlags::inBounds() | llvm::GEPNoWrapFlags::noUnsignedWrap()
1893+
, "", nullptr, nw
18891894
#endif
18901895
);
18911896
ty = DtoType(vd->type);
@@ -1903,8 +1908,7 @@ DLValue *DtoIndexAggregate(LLValue *src, AggregateDeclaration *ad,
19031908
}
19041909
ptr = DtoGEP(st, ptr, 0, off
19051910
#if LDC_LLVM_VER >= 2000
1906-
, "", nullptr
1907-
, llvm::GEPNoWrapFlags::inBounds() | llvm::GEPNoWrapFlags::noUnsignedWrap()
1911+
, "", nullptr, nw
19081912
#endif
19091913
);
19101914
ty = isaStruct(st)->getElementType(off);

gen/toir.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "dmd/root/rmem.h"
2323
#include "dmd/target.h"
2424
#include "dmd/template.h"
25+
#include "driver/cl_options.h"
26+
#include "gen/aa.h"
2527
#include "gen/abi/abi.h"
2628
#include "gen/arrays.h"
2729
#include "gen/binops.h"
@@ -1180,7 +1182,7 @@ class ToElemVisitor : public Visitor {
11801182
LLType *arrty = llvm::ArrayType::get(elt, e1type->isTypeSArray()->dim->isIntegerExp()->getInteger());
11811183
#if LDC_LLVM_VER >= 2000
11821184
llvm::GEPNoWrapFlags nw = llvm::GEPNoWrapFlags::inBounds();
1183-
if (e->indexIsInBounds)
1185+
if (e->indexIsInBounds && opts::enableGetElementPtrNuw)
11841186
nw |= llvm::GEPNoWrapFlags::noUnsignedWrap();
11851187
#endif
11861188
arrptr = DtoGEP(arrty, DtoLVal(l), DtoConstUint(0), DtoRVal(r)
@@ -1282,7 +1284,7 @@ class ToElemVisitor : public Visitor {
12821284
// offset by lower
12831285
#if LDC_LLVM_VER >= 2000
12841286
llvm::GEPNoWrapFlags nw = llvm::GEPNoWrapFlags::inBounds();
1285-
if (!needCheckUpper && !needCheckLower)
1287+
if (!needCheckUpper && !needCheckLower && opts::enableGetElementPtrNuw)
12861288
nw |= llvm::GEPNoWrapFlags::noUnsignedWrap();
12871289
#endif
12881290
eptr = DtoGEP1(DtoMemType(etype->nextOf()), getBasePointer(), vlo, "lowerbound"

tests/codegen/inbounds.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %ldc -c -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
1+
// RUN: %ldc -enable-getelementptr-nuw -c -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
22

33
struct S {
44
float x, y;

0 commit comments

Comments
 (0)