Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions clang/test/CodeGenSYCL/simplifycfg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %clangxx -fsycl -fsycl-device-only -flegacy-pass-manager %s -O3 -S -o - | FileCheck %s
// RUN: %clangxx -fsycl -fsycl-device-only -fno-legacy-pass-manager %s -O3 -S -o - | FileCheck %s
//
// This test checks that foo (which is @_Z3foov) is called twice after O3 optimizations.
//
// Usually clang with SimplifyCFG pass optimizes constructs like:
// if (i % 2 == 0)
// func();
// else
// func();
//
// into one simple func() invocation.
// This behaviour might be wrong in cases when func's behaviour depends on
// a place where it is written.
// There is a relevant discussion about introducing
// a reliable tool for such cases: https://reviews.llvm.org/D85603

// CHECK: tail call spir_func void @_Z3foov()
// CHECK: tail call spir_func void @_Z3foov()

SYCL_EXTERNAL void foo();

SYCL_EXTERNAL void bar(int i) {
if (i % 2 == 0) {
foo();
} else {
foo();
}
}

11 changes: 9 additions & 2 deletions llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ extern cl::opt<bool> EnableMatrix;

extern cl::opt<bool> DisablePreInliner;
extern cl::opt<int> PreInlineThreshold;

extern cl::opt<bool> SYCLOptimizationMode;
} // namespace llvm

void PassBuilder::invokePeepholeEPCallbacks(FunctionPassManager &FPM,
Expand Down Expand Up @@ -575,8 +577,12 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
for (auto &C : ScalarOptimizerLateEPCallbacks)
C(FPM, Level);

FPM.addPass(SimplifyCFGPass(
if (SYCLOptimizationMode)
FPM.addPass(SimplifyCFGPass());
else
FPM.addPass(SimplifyCFGPass(
SimplifyCFGOptions().hoistCommonInsts(true).sinkCommonInsts(true)));

FPM.addPass(InstCombinePass());
invokePeepholeEPCallbacks(FPM, Level);

Expand Down Expand Up @@ -1029,7 +1035,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
// convert to more optimized IR using more aggressive simplify CFG options.
// The extra sinking transform can create larger basic blocks, so do this
// before SLP vectorization.
FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions()
if (!SYCLOptimizationMode)
FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions()
.forwardSwitchCondToPhi(true)
.convertSwitchToLookupTable(true)
.needCanonicalLoops(false)
Expand Down
26 changes: 15 additions & 11 deletions llvm/lib/Transforms/IPO/PassManagerBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ static cl::opt<bool>
RunLoopRerolling("reroll-loops", cl::Hidden,
cl::desc("Run the loop rerolling pass"));

static cl::opt<bool>
SYCLOptimizationMode("sycl-opt", cl::init(false), cl::Hidden,
cl::desc("Enable SYCL optimization mode."));
cl::opt<bool> SYCLOptimizationMode("sycl-opt", cl::init(false), cl::Hidden,
cl::desc("Enable SYCL optimization mode."));

cl::opt<bool> RunNewGVN("enable-newgvn", cl::init(false), cl::Hidden,
cl::desc("Run the NewGVN pass"));
Expand Down Expand Up @@ -542,8 +541,12 @@ void PassManagerBuilder::addFunctionSimplificationPasses(
MPM.add(createLoopRerollPass());

// Merge & remove BBs and sink & hoist common instructions.
MPM.add(createCFGSimplificationPass(
SimplifyCFGOptions().hoistCommonInsts(true).sinkCommonInsts(true)));
if (SYCLOptimizationMode)
MPM.add(createCFGSimplificationPass());
else
MPM.add(createCFGSimplificationPass(
SimplifyCFGOptions().hoistCommonInsts(true).sinkCommonInsts(true)));

// Clean up after everything.
MPM.add(createInstructionCombiningPass());
addExtensionsToPM(EP_Peephole, MPM);
Expand Down Expand Up @@ -607,12 +610,13 @@ void PassManagerBuilder::addVectorPasses(legacy::PassManagerBase &PM,
// convert to more optimized IR using more aggressive simplify CFG options.
// The extra sinking transform can create larger basic blocks, so do this
// before SLP vectorization.
PM.add(createCFGSimplificationPass(SimplifyCFGOptions()
.forwardSwitchCondToPhi(true)
.convertSwitchToLookupTable(true)
.needCanonicalLoops(false)
.hoistCommonInsts(true)
.sinkCommonInsts(true)));
if (!SYCLOptimizationMode)
PM.add(createCFGSimplificationPass(SimplifyCFGOptions()
.forwardSwitchCondToPhi(true)
.convertSwitchToLookupTable(true)
.needCanonicalLoops(false)
.hoistCommonInsts(true)
.sinkCommonInsts(true)));

if (IsFullLTO) {
PM.add(createSCCPPass()); // Propagate exposed constants
Expand Down