Skip to content

Commit 87ec30c

Browse files
[MLIR][Flang][OpenMP] Remove unused global variables in offload target module (llvm#97)
1 parent 362d217 commit 87ec30c

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ std::unique_ptr<mlir::Pass> createAlgebraicSimplificationPass();
7676
std::unique_ptr<mlir::Pass>
7777
createAlgebraicSimplificationPass(const mlir::GreedyRewriteConfig &config);
7878

79+
std::unique_ptr<mlir::Pass> createOMPGlobalFilteringPass();
7980
std::unique_ptr<mlir::Pass> createVScaleAttrPass();
8081
std::unique_ptr<mlir::Pass>
8182
createVScaleAttrPass(std::pair<unsigned, unsigned> vscaleAttr);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,16 @@ def OMPFunctionFiltering : Pass<"omp-function-filtering"> {
358358
];
359359
}
360360

361+
def OMPGlobalFiltering : Pass<"omp-global-filtering"> {
362+
let summary = "Filters out globals intended for the host when compiling "
363+
"for the target device.";
364+
let constructor = "::fir::createOMPGlobalFilteringPass()";
365+
let dependentDialects = [
366+
"mlir::func::FuncDialect",
367+
"fir::FIROpsDialect"
368+
];
369+
}
370+
361371
def VScaleAttr : Pass<"vscale-attr", "mlir::func::FuncOp"> {
362372
let summary = "Add vscale_range attribute to functions";
363373
let description = [{

flang/include/flang/Tools/CLOptions.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,10 @@ inline void createOpenMPFIRPassPipeline(mlir::PassManager &pm,
355355
pm, fir::createOMPMapInfoFinalizationPass);
356356

357357
pm.addPass(fir::createOMPMarkDeclareTargetPass());
358-
if (isTargetDevice)
358+
if (isTargetDevice) {
359359
pm.addPass(fir::createOMPFunctionFiltering());
360+
pm.addPass(fir::createOMPGlobalFilteringPass());
361+
}
360362
}
361363

362364
#if !defined(FLANG_EXCLUDE_CODEGEN)

flang/lib/Optimizer/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ add_flang_library(FIRTransforms
1919
PolymorphicOpConversion.cpp
2020
LoopVersioning.cpp
2121
OMPFunctionFiltering.cpp
22+
OMPGlobalFiltering.cpp
2223
OMPMapInfoFinalization.cpp
2324
OMPMarkDeclareTarget.cpp
2425
StackReclaim.cpp
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//===- OMPFunctionFiltering.cpp -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements transforms to filter out functions intended for the host
10+
// when compiling for the device and vice versa.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "flang/Optimizer/Dialect/FIRDialect.h"
15+
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
16+
#include "flang/Optimizer/Transforms/Passes.h"
17+
18+
#include "mlir/Dialect/Func/IR/FuncOps.h"
19+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
20+
#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
21+
#include "mlir/IR/BuiltinOps.h"
22+
#include "llvm/ADT/SmallVector.h"
23+
24+
namespace fir {
25+
#define GEN_PASS_DEF_OMPGLOBALFILTERING
26+
#include "flang/Optimizer/Transforms/Passes.h.inc"
27+
} // namespace fir
28+
29+
using namespace mlir;
30+
31+
namespace {
32+
class OMPGlobalFilteringPass
33+
: public fir::impl::OMPGlobalFilteringBase<OMPGlobalFilteringPass> {
34+
public:
35+
OMPGlobalFilteringPass() = default;
36+
37+
void runOnOperation() override {
38+
auto op = dyn_cast<omp::OffloadModuleInterface>(getOperation());
39+
if (!op || !op.getIsTargetDevice())
40+
return;
41+
42+
op->walk<WalkOrder::PreOrder>([&](fir::GlobalOp globalOp) {
43+
bool symbolUnused = true;
44+
SymbolTable::UseRange globalUses = *globalOp.getSymbolUses(op);
45+
for (SymbolTable::SymbolUse use : globalUses) {
46+
if (use.getUser() == globalOp)
47+
continue;
48+
symbolUnused = false;
49+
break;
50+
}
51+
52+
// Remove unused host symbols with external linkage
53+
// TODO: Add support for declare target global variables
54+
if (symbolUnused && !globalOp.getLinkName())
55+
globalOp.erase();
56+
return WalkResult::advance();
57+
});
58+
}
59+
};
60+
} // namespace
61+
62+
std::unique_ptr<Pass> fir::createOMPGlobalFilteringPass() {
63+
return std::make_unique<OMPGlobalFilteringPass>();
64+
}

0 commit comments

Comments
 (0)