Skip to content

Commit a88677e

Browse files
authored
Reland "[flang] Integrate the option -flang-experimental-integer-overflow into -fno-wrapv" (#118933)
This relands #110063. The performance issue on 503.bwaves_r is found not to be related to the patch, and is resolved by fbd89bc when LTO is enabled.
1 parent 411196b commit a88677e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+144
-704
lines changed

clang/include/clang/Driver/Options.td

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

6843-
def flang_experimental_integer_overflow : Flag<["-"], "flang-experimental-integer-overflow">,
6844-
Flags<[HelpHidden]>, Visibility<[FlangOption, FC1Option]>,
6845-
HelpText<"Add nsw flag to internal operations such as do-variable increment (experimental)">;
6846-
68476843
//===----------------------------------------------------------------------===//
68486844
// FLangOption + CoreOption + NoXarchOption
68496845
//===----------------------------------------------------------------------===//

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: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,5 @@ ENUM_LOWERINGOPT(Underscoring, unsigned, 1, 1)
3838
/// (i.e. wraps around as two's complement). Off by default.
3939
ENUM_LOWERINGOPT(IntegerWrapAround, unsigned, 1, 0)
4040

41-
/// If true, add nsw flags to loop variable increments.
42-
/// Off by default.
43-
/// TODO: integrate this option with the above
44-
ENUM_LOWERINGOPT(NSWOnLoopVarInc, unsigned, 1, 0)
45-
4641
#undef LOWERINGOPT
4742
#undef ENUM_LOWERINGOPT

flang/include/flang/Optimizer/Passes/Pipelines.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ namespace fir {
3333

3434
using PassConstructor = std::unique_ptr<mlir::Pass>();
3535

36-
template <typename OP>
37-
void addNestedPassToOps(mlir::PassManager &pm, PassConstructor ctor) {
36+
template <typename F, typename OP>
37+
void addNestedPassToOps(mlir::PassManager &pm, F ctor) {
3838
pm.addNestedPass<OP>(ctor());
3939
}
4040

41-
template <typename OP, typename... OPS,
41+
template <typename F, typename OP, typename... OPS,
4242
typename = std::enable_if_t<sizeof...(OPS) != 0>>
43-
void addNestedPassToOps(mlir::PassManager &pm, PassConstructor ctor) {
44-
addNestedPassToOps<OP>(pm, ctor);
45-
addNestedPassToOps<OPS...>(pm, ctor);
43+
void addNestedPassToOps(mlir::PassManager &pm, F ctor) {
44+
addNestedPassToOps<F, OP>(pm, ctor);
45+
addNestedPassToOps<F, OPS...>(pm, ctor);
4646
}
4747

4848
/// Generic for adding a pass to the pass manager if it is not disabled.
@@ -60,11 +60,12 @@ void addNestedPassConditionally(mlir::PassManager &pm,
6060
pm.addNestedPass<OP>(ctor());
6161
}
6262

63-
void addNestedPassToAllTopLevelOperations(mlir::PassManager &pm,
64-
PassConstructor ctor);
63+
template <typename F>
64+
void addNestedPassToAllTopLevelOperations(mlir::PassManager &pm, F ctor);
6565

66+
template <typename F>
6667
void addNestedPassToAllTopLevelOperationsConditionally(
67-
mlir::PassManager &pm, llvm::cl::opt<bool> &disabled, PassConstructor ctor);
68+
mlir::PassManager &pm, llvm::cl::opt<bool> &disabled, F ctor);
6869

6970
/// Add MLIR Canonicalizer pass with region simplification disabled.
7071
/// FIR does not support the promotion of some SSA value to block arguments (or

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ namespace fir {
6666
std::unique_ptr<mlir::Pass> createAffineDemotionPass();
6767
std::unique_ptr<mlir::Pass>
6868
createArrayValueCopyPass(fir::ArrayValueCopyOptions options = {});
69-
std::unique_ptr<mlir::Pass> createCFGConversionPassWithNSW();
7069
std::unique_ptr<mlir::Pass> createMemDataFlowOptPass();
7170
std::unique_ptr<mlir::Pass> createPromoteToAffinePass();
7271
std::unique_ptr<mlir::Pass>
@@ -83,7 +82,7 @@ createVScaleAttrPass(std::pair<unsigned, unsigned> vscaleAttr);
8382

8483
void populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
8584
bool forceLoopToExecuteOnce = false,
86-
bool setNSW = false);
85+
bool setNSW = true);
8786

8887
// declarative passes
8988
#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/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
bool EnableOpenMP = false; ///< Enable OpenMP lowering.
127127
};
128128

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,12 +1362,6 @@ bool CompilerInvocation::createFromArgs(
13621362
invoc.loweringOpts.setNoPPCNativeVecElemOrder(true);
13631363
}
13641364

1365-
// -flang-experimental-integer-overflow
1366-
if (args.hasArg(
1367-
clang::driver::options::OPT_flang_experimental_integer_overflow)) {
1368-
invoc.loweringOpts.setNSWOnLoopVarInc(true);
1369-
}
1370-
13711365
// Preserve all the remark options requested, i.e. -Rpass, -Rpass-missed or
13721366
// -Rpass-analysis. This will be used later when processing and outputting the
13731367
// 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
@@ -836,8 +836,8 @@ void CodeGenAction::generateLLVMIR() {
836836
Fortran::common::LanguageFeature::OpenMP))
837837
config.EnableOpenMP = true;
838838

839-
if (ci.getInvocation().getLoweringOpts().getNSWOnLoopVarInc())
840-
config.NSWOnLoopVarInc = true;
839+
if (ci.getInvocation().getLoweringOpts().getIntegerWrapAround())
840+
config.NSWOnLoopVarInc = false;
841841

842842
// Create the pass pipeline
843843
fir::createMLIRToLLVMPassPipeline(pm, config, getCurrentFile());

flang/lib/Lower/Bridge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2330,7 +2330,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
23302330
assert(!incrementLoopNestInfo.empty() && "empty loop nest");
23312331
mlir::Location loc = toLocation();
23322332
mlir::arith::IntegerOverflowFlags flags{};
2333-
if (getLoweringOptions().getNSWOnLoopVarInc())
2333+
if (!getLoweringOptions().getIntegerWrapAround())
23342334
flags = bitEnumSet(flags, mlir::arith::IntegerOverflowFlags::nsw);
23352335
auto iofAttr = mlir::arith::IntegerOverflowFlagsAttr::get(
23362336
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/Passes/Pipelines.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
namespace fir {
1515

16-
void addNestedPassToAllTopLevelOperations(mlir::PassManager &pm,
17-
PassConstructor ctor) {
18-
addNestedPassToOps<mlir::func::FuncOp, mlir::omp::DeclareReductionOp,
16+
template <typename F>
17+
void addNestedPassToAllTopLevelOperations(mlir::PassManager &pm, F ctor) {
18+
addNestedPassToOps<F, mlir::func::FuncOp, mlir::omp::DeclareReductionOp,
1919
mlir::omp::PrivateClauseOp, fir::GlobalOp>(pm, ctor);
2020
}
2121

@@ -25,11 +25,11 @@ void addPassToGPUModuleOperations(mlir::PassManager &pm, PassConstructor ctor) {
2525
nestPM.addNestedPass<mlir::gpu::GPUFuncOp>(ctor());
2626
}
2727

28+
template <typename F>
2829
void addNestedPassToAllTopLevelOperationsConditionally(
29-
mlir::PassManager &pm, llvm::cl::opt<bool> &disabled,
30-
PassConstructor ctor) {
30+
mlir::PassManager &pm, llvm::cl::opt<bool> &disabled, F ctor) {
3131
if (!disabled)
32-
addNestedPassToAllTopLevelOperations(pm, ctor);
32+
addNestedPassToAllTopLevelOperations<F>(pm, ctor);
3333
}
3434

3535
void addCanonicalizerPassWithoutRegionSimplification(mlir::OpPassManager &pm) {
@@ -40,12 +40,11 @@ void addCanonicalizerPassWithoutRegionSimplification(mlir::OpPassManager &pm) {
4040

4141
void addCfgConversionPass(mlir::PassManager &pm,
4242
const MLIRToLLVMPassPipelineConfig &config) {
43-
if (config.NSWOnLoopVarInc)
44-
addNestedPassToAllTopLevelOperationsConditionally(
45-
pm, disableCfgConversion, fir::createCFGConversionPassWithNSW);
46-
else
47-
addNestedPassToAllTopLevelOperationsConditionally(pm, disableCfgConversion,
48-
fir::createCFGConversion);
43+
fir::CFGConversionOptions options;
44+
if (!config.NSWOnLoopVarInc)
45+
options.setNSW = false;
46+
addNestedPassToAllTopLevelOperationsConditionally(
47+
pm, disableCfgConversion, [&]() { return createCFGConversion(options); });
4948
}
5049

5150
void addAVC(mlir::PassManager &pm, const llvm::OptimizationLevel &optLevel) {
@@ -166,7 +165,8 @@ void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm,
166165
config.enableRegionSimplification = mlir::GreedySimplifyRegionLevel::Disabled;
167166
pm.addPass(mlir::createCSEPass());
168167
fir::addAVC(pm, pc.OptLevel);
169-
addNestedPassToAllTopLevelOperations(pm, fir::createCharacterConversion);
168+
addNestedPassToAllTopLevelOperations<PassConstructor>(
169+
pm, fir::createCharacterConversion);
170170
pm.addPass(mlir::createCanonicalizerPass(config));
171171
pm.addPass(fir::createSimplifyRegionLite());
172172
if (pc.OptLevel.isOptimizingForSpeed()) {
@@ -200,7 +200,8 @@ void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm,
200200
if (pc.AliasAnalysis && !disableFirAliasTags && !useOldAliasTags)
201201
pm.addPass(fir::createAddAliasTags());
202202

203-
addNestedPassToAllTopLevelOperations(pm, fir::createStackReclaim);
203+
addNestedPassToAllTopLevelOperations<PassConstructor>(
204+
pm, fir::createStackReclaim);
204205
// convert control flow to CFG form
205206
fir::addCfgConversionPass(pm, pc);
206207
pm.addPass(mlir::createConvertSCFToCFPass());
@@ -222,15 +223,16 @@ void createHLFIRToFIRPassPipeline(mlir::PassManager &pm, bool enableOpenMP,
222223
llvm::OptimizationLevel optLevel) {
223224
if (optLevel.isOptimizingForSpeed()) {
224225
addCanonicalizerPassWithoutRegionSimplification(pm);
225-
addNestedPassToAllTopLevelOperations(pm,
226-
hlfir::createSimplifyHLFIRIntrinsics);
226+
addNestedPassToAllTopLevelOperations<PassConstructor>(
227+
pm, hlfir::createSimplifyHLFIRIntrinsics);
227228
}
228-
addNestedPassToAllTopLevelOperations(pm, hlfir::createInlineElementals);
229+
addNestedPassToAllTopLevelOperations<PassConstructor>(
230+
pm, hlfir::createInlineElementals);
229231
if (optLevel.isOptimizingForSpeed()) {
230232
addCanonicalizerPassWithoutRegionSimplification(pm);
231233
pm.addPass(mlir::createCSEPass());
232-
addNestedPassToAllTopLevelOperations(pm,
233-
hlfir::createOptimizedBufferization);
234+
addNestedPassToAllTopLevelOperations<PassConstructor>(
235+
pm, hlfir::createOptimizedBufferization);
234236
}
235237
pm.addPass(hlfir::createLowerHLFIROrderedAssignments());
236238
pm.addPass(hlfir::createLowerHLFIRIntrinsics());
@@ -270,7 +272,8 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
270272
MLIRToLLVMPassPipelineConfig config,
271273
llvm::StringRef inputFilename) {
272274
fir::addBoxedProcedurePass(pm);
273-
addNestedPassToAllTopLevelOperations(pm, fir::createAbstractResultOpt);
275+
addNestedPassToAllTopLevelOperations<PassConstructor>(
276+
pm, fir::createAbstractResultOpt);
274277
addPassToGPUModuleOperations(pm, fir::createAbstractResultOpt);
275278
fir::addCodeGenRewritePass(
276279
pm, (config.DebugInfo != llvm::codegenoptions::NoDebugInfo));

flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,6 @@ class CfgConversion : public fir::impl::CFGConversionBase<CfgConversion> {
332332
public:
333333
using CFGConversionBase<CfgConversion>::CFGConversionBase;
334334

335-
CfgConversion(bool setNSW) { this->setNSW = setNSW; }
336-
337335
void runOnOperation() override {
338336
auto *context = &this->getContext();
339337
mlir::RewritePatternSet patterns(context);
@@ -365,7 +363,3 @@ void fir::populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
365363
patterns.insert<CfgLoopConv, CfgIfConv, CfgIterWhileConv>(
366364
patterns.getContext(), forceLoopToExecuteOnce, setNSW);
367365
}
368-
369-
std::unique_ptr<mlir::Pass> fir::createCFGConversionPassWithNSW() {
370-
return std::make_unique<CfgConversion>(true);
371-
}

flang/test/Driver/frontend-forwarding.f90

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
! RUN: -fversion-loops-for-stride \
2121
! RUN: -flang-experimental-hlfir \
2222
! RUN: -flang-deprecated-no-hlfir \
23-
! RUN: -flang-experimental-integer-overflow \
2423
! RUN: -fno-ppc-native-vector-element-order \
2524
! RUN: -fppc-native-vector-element-order \
2625
! RUN: -mllvm -print-before-all \
@@ -52,7 +51,6 @@
5251
! CHECK: "-fversion-loops-for-stride"
5352
! CHECK: "-flang-experimental-hlfir"
5453
! CHECK: "-flang-deprecated-no-hlfir"
55-
! CHECK: "-flang-experimental-integer-overflow"
5654
! CHECK: "-fno-ppc-native-vector-element-order"
5755
! CHECK: "-fppc-native-vector-element-order"
5856
! CHECK: "-Rpass"

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,10 @@ func.func @_QPopenmp_target_data_region() {
358358
%9 = arith.subi %8, %c1_i64 : i64
359359
%10 = fir.coordinate_of %0, %9 : (!fir.ref<!fir.array<1024xi32>>, i64) -> !fir.ref<i32>
360360
fir.store %6 to %10 : !fir.ref<i32>
361-
%11 = arith.addi %arg0, %c1 : index
361+
%11 = arith.addi %arg0, %c1 overflow<nsw> : index
362362
%12 = fir.convert %c1 : (index) -> i32
363363
%13 = fir.load %1 : !fir.ref<i32>
364-
%14 = arith.addi %13, %12 : i32
364+
%14 = arith.addi %13, %12 overflow<nsw> : i32
365365
fir.result %11, %14 : index, i32
366366
}
367367
fir.store %5#1 to %1 : !fir.ref<i32>
@@ -404,11 +404,11 @@ func.func @_QPopenmp_target_data_region() {
404404
// CHECK: %[[VAL_21:.*]] = llvm.sub %[[VAL_19]], %[[VAL_20]] : i64
405405
// CHECK: %[[VAL_22:.*]] = llvm.getelementptr %[[VAL_1]][0, %[[VAL_21]]] : (!llvm.ptr, i64) -> !llvm.ptr
406406
// CHECK: llvm.store %[[VAL_17]], %[[VAL_22]] : i32, !llvm.ptr
407-
// CHECK: %[[VAL_23:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] : i64
407+
// CHECK: %[[VAL_23:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] overflow<nsw> : i64
408408
// CHECK: %[[VAL_24:.*]] = llvm.trunc %[[VAL_8]] : i64 to i32
409409
// CHECK: %[[VAL_25:.*]] = llvm.load %[[VAL_3]] : !llvm.ptr -> i32
410-
// CHECK: %[[VAL_26:.*]] = llvm.add %[[VAL_25]], %[[VAL_24]] : i32
411-
// CHECK: %[[VAL_27:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] : i64
410+
// CHECK: %[[VAL_26:.*]] = llvm.add %[[VAL_25]], %[[VAL_24]] overflow<nsw> : i32
411+
// CHECK: %[[VAL_27:.*]] = llvm.add %[[VAL_12]], %[[VAL_8]] overflow<nsw> : i64
412412
// CHECK: %[[VAL_28:.*]] = llvm.mlir.constant(1 : index) : i64
413413
// CHECK: %[[VAL_29:.*]] = llvm.sub %[[VAL_14]], %[[VAL_28]] : i64
414414
// CHECK: llvm.br ^bb1(%[[VAL_27]], %[[VAL_26]], %[[VAL_29]] : i64, i32, i64)

0 commit comments

Comments
 (0)