Skip to content

Commit c14087b

Browse files
committed
[flang] integrate the option -flang-experimental-integer-overflow into -fno-wrapv
nsw is added to do-variable increment when -fno-wrapv is enabled as GFortran seems to do. Therefore, the option introduced by PR91579 isn't necessary any more. Note that the feature of -flang-experimental-integer-overflow is now enabled by default.
1 parent f43ad88 commit c14087b

40 files changed

+641
-652
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6742,10 +6742,6 @@ def flang_deprecated_no_hlfir : Flag<["-"], "flang-deprecated-no-hlfir">,
67426742
Flags<[HelpHidden]>, Visibility<[FlangOption, FC1Option]>,
67436743
HelpText<"Do not use HLFIR lowering (deprecated)">;
67446744

6745-
def flang_experimental_integer_overflow : Flag<["-"], "flang-experimental-integer-overflow">,
6746-
Flags<[HelpHidden]>, Visibility<[FlangOption, FC1Option]>,
6747-
HelpText<"Add nsw flag to internal operations such as do-variable increment (experimental)">;
6748-
67496745
//===----------------------------------------------------------------------===//
67506746
// FLangOption + CoreOption + NoXarchOption
67516747
//===----------------------------------------------------------------------===//

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ void Flang::addCodegenOptions(const ArgList &Args,
148148

149149
Args.addAllArgs(CmdArgs, {options::OPT_flang_experimental_hlfir,
150150
options::OPT_flang_deprecated_no_hlfir,
151-
options::OPT_flang_experimental_integer_overflow,
152151
options::OPT_fno_ppc_native_vec_elem_order,
153152
options::OPT_fppc_native_vec_elem_order});
154153
}

flang/include/flang/Lower/LoweringOptions.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ ENUM_LOWERINGOPT(NoPPCNativeVecElemOrder, unsigned, 1, 0)
3434
/// On by default.
3535
ENUM_LOWERINGOPT(Underscoring, unsigned, 1, 1)
3636

37-
/// If true, add nsw flags to loop variable increments.
38-
/// Off by default.
39-
ENUM_LOWERINGOPT(NSWOnLoopVarInc, unsigned, 1, 0)
37+
/// If true, assume the behavior of integer overflow is defined
38+
/// (i.e. wraps around as two's complement). Off by default.
39+
ENUM_LOWERINGOPT(IntegerWrapAround, unsigned, 1, 0)
4040

4141
#undef LOWERINGOPT
4242
#undef ENUM_LOWERINGOPT

flang/include/flang/Optimizer/Transforms/Passes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace fir {
6565
std::unique_ptr<mlir::Pass> createAffineDemotionPass();
6666
std::unique_ptr<mlir::Pass>
6767
createArrayValueCopyPass(fir::ArrayValueCopyOptions options = {});
68-
std::unique_ptr<mlir::Pass> createCFGConversionPassWithNSW();
68+
std::unique_ptr<mlir::Pass> createCFGConversionPassWithoutNSW();
6969
std::unique_ptr<mlir::Pass> createMemDataFlowOptPass();
7070
std::unique_ptr<mlir::Pass> createPromoteToAffinePass();
7171
std::unique_ptr<mlir::Pass>
@@ -82,7 +82,7 @@ createVScaleAttrPass(std::pair<unsigned, unsigned> vscaleAttr);
8282

8383
void populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
8484
bool forceLoopToExecuteOnce = false,
85-
bool setNSW = false);
85+
bool setNSW = true);
8686

8787
// declarative passes
8888
#define GEN_PASS_REGISTRATION

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def CFGConversion : Pass<"cfg-conversion"> {
153153
/*default=*/"false",
154154
"force the body of a loop to execute at least once">,
155155
Option<"setNSW", "set-nsw", "bool",
156-
/*default=*/"false",
156+
/*default=*/"true",
157157
"set nsw on loop variable increment">
158158
];
159159
}

flang/include/flang/Tools/CLOptions.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ inline void addCfgConversionPass(
162162
mlir::PassManager &pm, const MLIRToLLVMPassPipelineConfig &config) {
163163
if (config.NSWOnLoopVarInc)
164164
addNestedPassToAllTopLevelOperationsConditionally(
165-
pm, disableCfgConversion, fir::createCFGConversionPassWithNSW);
165+
pm, disableCfgConversion, fir::createCFGConversion);
166166
else
167167
addNestedPassToAllTopLevelOperationsConditionally(
168-
pm, disableCfgConversion, fir::createCFGConversion);
168+
pm, disableCfgConversion, fir::createCFGConversionPassWithoutNSW);
169169
}
170170

171171
inline void addAVC(

flang/include/flang/Tools/CrossToolHelpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
122122
bool NoSignedZerosFPMath =
123123
false; ///< Set no-signed-zeros-fp-math attribute for functions.
124124
bool UnsafeFPMath = false; ///< Set unsafe-fp-math attribute for functions.
125-
bool NSWOnLoopVarInc = false; ///< Add nsw flag to loop variable increments.
125+
bool NSWOnLoopVarInc = true; ///< Add nsw flag to loop variable increments.
126126
};
127127

128128
struct OffloadModuleOpts {

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,12 +1305,6 @@ bool CompilerInvocation::createFromArgs(
13051305
invoc.loweringOpts.setNoPPCNativeVecElemOrder(true);
13061306
}
13071307

1308-
// -flang-experimental-integer-overflow
1309-
if (args.hasArg(
1310-
clang::driver::options::OPT_flang_experimental_integer_overflow)) {
1311-
invoc.loweringOpts.setNSWOnLoopVarInc(true);
1312-
}
1313-
13141308
// Preserve all the remark options requested, i.e. -Rpass, -Rpass-missed or
13151309
// -Rpass-analysis. This will be used later when processing and outputting the
13161310
// remarks generated by LLVM in ExecuteCompilerInvocation.cpp.

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,8 +829,8 @@ void CodeGenAction::generateLLVMIR() {
829829
config.VScaleMax = vsr->second;
830830
}
831831

832-
if (ci.getInvocation().getLoweringOpts().getNSWOnLoopVarInc())
833-
config.NSWOnLoopVarInc = true;
832+
if (ci.getInvocation().getLoweringOpts().getIntegerWrapAround())
833+
config.NSWOnLoopVarInc = false;
834834

835835
// Create the pass pipeline
836836
fir::createMLIRToLLVMPassPipeline(pm, config, getCurrentFile());

flang/lib/Lower/Bridge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
22712271
assert(!incrementLoopNestInfo.empty() && "empty loop nest");
22722272
mlir::Location loc = toLocation();
22732273
mlir::arith::IntegerOverflowFlags flags{};
2274-
if (getLoweringOptions().getNSWOnLoopVarInc())
2274+
if (!getLoweringOptions().getIntegerWrapAround())
22752275
flags = bitEnumSet(flags, mlir::arith::IntegerOverflowFlags::nsw);
22762276
auto iofAttr = mlir::arith::IntegerOverflowFlagsAttr::get(
22772277
builder->getContext(), flags);

flang/lib/Lower/IO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ static void genIoLoop(Fortran::lower::AbstractConverter &converter,
929929
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
930930
mlir::Location loc = converter.getCurrentLocation();
931931
mlir::arith::IntegerOverflowFlags flags{};
932-
if (converter.getLoweringOptions().getNSWOnLoopVarInc())
932+
if (!converter.getLoweringOptions().getIntegerWrapAround())
933933
flags = bitEnumSet(flags, mlir::arith::IntegerOverflowFlags::nsw);
934934
auto iofAttr =
935935
mlir::arith::IntegerOverflowFlagsAttr::get(builder.getContext(), flags);

flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,6 @@ void fir::populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
366366
patterns.getContext(), forceLoopToExecuteOnce, setNSW);
367367
}
368368

369-
std::unique_ptr<mlir::Pass> fir::createCFGConversionPassWithNSW() {
370-
return std::make_unique<CfgConversion>(true);
369+
std::unique_ptr<mlir::Pass> fir::createCFGConversionPassWithoutNSW() {
370+
return std::make_unique<CfgConversion>(false);
371371
}

flang/test/Driver/frontend-forwarding.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
! RUN: -fno-signed-zeros \
1515
! RUN: -fassociative-math \
1616
! RUN: -freciprocal-math \
17+
! RUN: -fno-strict-overflow \
1718
! RUN: -fomit-frame-pointer \
1819
! RUN: -fpass-plugin=Bye%pluginext \
1920
! RUN: -fversion-loops-for-stride \
2021
! RUN: -flang-experimental-hlfir \
2122
! RUN: -flang-deprecated-no-hlfir \
22-
! RUN: -flang-experimental-integer-overflow \
2323
! RUN: -fno-ppc-native-vector-element-order \
2424
! RUN: -fppc-native-vector-element-order \
2525
! RUN: -mllvm -print-before-all \
@@ -51,7 +51,6 @@
5151
! CHECK: "-fversion-loops-for-stride"
5252
! CHECK: "-flang-experimental-hlfir"
5353
! CHECK: "-flang-deprecated-no-hlfir"
54-
! CHECK: "-flang-experimental-integer-overflow"
5554
! CHECK: "-fno-ppc-native-vector-element-order"
5655
! CHECK: "-fppc-native-vector-element-order"
5756
! CHECK: "-Rpass"
@@ -63,4 +62,5 @@
6362
! CHECK: "-Rpass=inline"
6463
! CHECK: "-mframe-pointer=none"
6564
! CHECK: "-mllvm" "-print-before-all"
65+
! CHECK: "-fwrapv"
6666
! CHECK: "-save-temps=obj"

flang/test/Fir/convert-to-llvm-openmp-and-fir.fir

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,10 @@ func.func @_QPopenmp_target_data_region() {
364364
%9 = arith.subi %8, %c1_i64 : i64
365365
%10 = fir.coordinate_of %0, %9 : (!fir.ref<!fir.array<1024xi32>>, i64) -> !fir.ref<i32>
366366
fir.store %6 to %10 : !fir.ref<i32>
367-
%11 = arith.addi %arg0, %c1 : index
367+
%11 = arith.addi %arg0, %c1 overflow<nsw> : index
368368
%12 = fir.convert %c1 : (index) -> i32
369369
%13 = fir.load %1 : !fir.ref<i32>
370-
%14 = arith.addi %13, %12 : i32
370+
%14 = arith.addi %13, %12 overflow<nsw> : i32
371371
fir.result %11, %14 : index, i32
372372
}
373373
fir.store %5#1 to %1 : !fir.ref<i32>
@@ -410,11 +410,11 @@ func.func @_QPopenmp_target_data_region() {
410410
// CHECK: %[[VAL_21:.*]] = llvm.sub %[[VAL_19]], %[[VAL_20]] : i64
411411
// CHECK: %[[VAL_22:.*]] = llvm.getelementptr %[[VAL_1]][0, %[[VAL_21]]] : (!llvm.ptr, i64) -> !llvm.ptr
412412
// CHECK: llvm.store %[[VAL_17]], %[[VAL_22]] : i32, !llvm.ptr
413-
// CHECK: %[[VAL_23:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] : i64
413+
// CHECK: %[[VAL_23:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] overflow<nsw> : i64
414414
// CHECK: %[[VAL_24:.*]] = llvm.trunc %[[VAL_8]] : i64 to i32
415415
// CHECK: %[[VAL_25:.*]] = llvm.load %[[VAL_3]] : !llvm.ptr -> i32
416-
// CHECK: %[[VAL_26:.*]] = llvm.add %[[VAL_25]], %[[VAL_24]] : i32
417-
// CHECK: %[[VAL_27:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] : i64
416+
// CHECK: %[[VAL_26:.*]] = llvm.add %[[VAL_25]], %[[VAL_24]] overflow<nsw> : i32
417+
// CHECK: %[[VAL_27:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] overflow<nsw> : i64
418418
// CHECK: %[[VAL_28:.*]] = llvm.mlir.constant(1 : index) : i64
419419
// CHECK: %[[VAL_29:.*]] = llvm.sub %[[VAL_14]], %[[VAL_28]] : i64
420420
// CHECK: llvm.br ^bb1(%[[VAL_27]], %[[VAL_26]], %[[VAL_29]] : i64, i32, i64)

0 commit comments

Comments
 (0)