Skip to content

Commit e29f03a

Browse files
committed
Add enable-getelementptr-nuw flag
1 parent 3ce0b15 commit e29f03a

5 files changed

Lines changed: 19 additions & 7 deletions

File tree

driver/cl_options.cpp

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

724+
cl::opt<bool>
725+
enableGetElementPtrNuw("enable-getelementptr-nuw", cl::ZeroOrMore,
726+
cl::desc("enable nuw(no-unsigned-wrap) flag to "
727+
"LLVM's getelementptr insturction"));
728+
724729
#if LDC_LLVM_SUPPORTED_TARGET_SPIRV || LDC_LLVM_SUPPORTED_TARGET_NVPTX
725730
cl::list<std::string>
726731
dcomputeTargets("mdcompute-targets", cl::CommaSeparated,

driver/cl_options.h

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

139139
extern cl::opt<unsigned> fWarnStackSize;
140140

141+
extern cl::opt<bool> enableGetElementPtrNuw;
142+
141143
#if LDC_LLVM_SUPPORTED_TARGET_SPIRV || LDC_LLVM_SUPPORTED_TARGET_NVPTX
142144
extern cl::list<std::string> dcomputeTargets;
143145
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"
@@ -1868,12 +1869,16 @@ DLValue *DtoIndexAggregate(LLValue *src, AggregateDeclaration *ad,
18681869

18691870
LLValue *ptr = src;
18701871
LLType * ty = nullptr;
1872+
#if LDC_LLVM_VER >= 2000
1873+
llvm::GEPNoWrapFlags nw = llvm::GEPNoWrapFlags::inBounds();
1874+
if (opts::enableGetElementPtrNuw)
1875+
nw |= llvm::GEPNoWrapFlags::noUnsignedWrap();
1876+
#endif
18711877
if (!isFieldIdx) {
18721878
// apply byte-wise offset from object start
18731879
ptr = DtoGEP1(getI8Type(), ptr, off
18741880
#if LDC_LLVM_VER >= 2000
1875-
, "", nullptr
1876-
, llvm::GEPNoWrapFlags::inBounds() | llvm::GEPNoWrapFlags::noUnsignedWrap()
1881+
, "", nullptr, nw
18771882
#endif
18781883
);
18791884
ty = DtoType(vd->type);
@@ -1891,8 +1896,7 @@ DLValue *DtoIndexAggregate(LLValue *src, AggregateDeclaration *ad,
18911896
}
18921897
ptr = DtoGEP(st, ptr, 0, off
18931898
#if LDC_LLVM_VER >= 2000
1894-
, "", nullptr
1895-
, llvm::GEPNoWrapFlags::inBounds() | llvm::GEPNoWrapFlags::noUnsignedWrap()
1899+
, "", nullptr, nw
18961900
#endif
18971901
);
18981902
ty = isaStruct(st)->getElementType(off);

gen/toir.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "dmd/root/rmem.h"
2323
#include "dmd/target.h"
2424
#include "dmd/template.h"
25+
#include "driver/cl_options.h"
2526
#include "gen/aa.h"
2627
#include "gen/abi/abi.h"
2728
#include "gen/arrays.h"
@@ -1200,7 +1201,7 @@ class ToElemVisitor : public Visitor {
12001201
LLType *arrty = llvm::ArrayType::get(elt, e1type->isTypeSArray()->dim->isIntegerExp()->getInteger());
12011202
#if LDC_LLVM_VER >= 2000
12021203
llvm::GEPNoWrapFlags nw = llvm::GEPNoWrapFlags::inBounds();
1203-
if (e->indexIsInBounds)
1204+
if (e->indexIsInBounds && opts::enableGetElementPtrNuw)
12041205
nw |= llvm::GEPNoWrapFlags::noUnsignedWrap();
12051206
#endif
12061207
arrptr = DtoGEP(arrty, DtoLVal(l), DtoConstUint(0), DtoRVal(r)
@@ -1303,7 +1304,7 @@ class ToElemVisitor : public Visitor {
13031304
// offset by lower
13041305
#if LDC_LLVM_VER >= 2000
13051306
llvm::GEPNoWrapFlags nw = llvm::GEPNoWrapFlags::inBounds();
1306-
if (!needCheckUpper && !needCheckLower)
1307+
if (!needCheckUpper && !needCheckLower && opts::enableGetElementPtrNuw)
13071308
nw |= llvm::GEPNoWrapFlags::noUnsignedWrap();
13081309
#endif
13091310
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)