23#define GEN_PASS_DEF_HOSTOPFILTERINGPASS
24#include "mlir/Dialect/OpenMP/Transforms/Passes.h.inc"
50 for (
Value member : mapOp.getMembers())
51 collectRewrite(cast<omp::MapInfoOp>(member.getDefiningOp()), rewrites);
53 rewrites.insert(mapOp);
63 if ((isa<BlockArgument>(value) &&
64 isa<FunctionOpInterface>(
65 cast<BlockArgument>(value).getOwner()->getParentOp())) ||
66 rewrites.contains(value))
74 rewrites.insert(value);
79static std::optional<omp::DeclareTargetDeviceType>
81 auto declareTargetOp = dyn_cast<omp::DeclareTargetInterface>(op);
82 omp::DeclareTargetAttr declareTargetAttr =
83 declareTargetOp ? declareTargetOp.getDeclareTarget() :
nullptr;
84 if (declareTargetAttr)
85 return declareTargetAttr.getDeviceType();
90class HostOpFilteringPass
93 HostOpFilteringPass() =
default;
95 void runOnOperation()
override {
96 auto op = dyn_cast<omp::OffloadModuleInterface>(getOperation());
97 if (!op || !op.getIsTargetDevice())
100 op->walk<WalkOrder::PreOrder>([&](LLVM::LLVMFuncOp funcOp) {
101 omp::DeclareTargetDeviceType declareType =
103 .value_or(omp::DeclareTargetDeviceType::host);
106 if (funcOp.isExternal() ||
107 declareType != omp::DeclareTargetDeviceType::host)
110 if (
failed(rewriteHostFunction(funcOp))) {
111 funcOp.emitOpError() <<
"could not filter host-only operations";
121 op->walk([&](LLVM::GlobalOp globalOp) {
123 globalOp.setLinkage(LLVM::Linkage::Internal);
156 LogicalResult rewriteHostFunction(LLVM::LLVMFuncOp funcOp) {
157 Region ®ion = funcOp.getFunctionBody();
158 LLVM::LLVMFunctionType functionType = funcOp.getFunctionType();
164 if (
auto targetOp = dyn_cast<omp::TargetOp>(op)) {
165 targetOps.push_back(targetOp);
173 if (
auto targetDataOp = dyn_cast<omp::TargetDataOp>(op)) {
175 cast<omp::BlockArgOpenMPOpInterface>(*targetDataOp)
176 .getBlockArgsPairs(argPairs);
177 for (
auto [operand, blockArg] : argPairs) {
178 auto mapInfo = cast<omp::MapInfoOp>(operand.getDefiningOp());
179 blockArg.replaceAllUsesWith(mapInfo.getVarPtr());
190 builder.setInsertionPointAfter(funcOp);
196 llvm::transform(region.
getArguments(), std::back_inserter(locs),
200 for (
auto [oldArg, newArg] :
202 oldArg.replaceAllUsesWith(newArg);
210 for (omp::TargetOp targetOp : targetOps) {
211 assert(targetOp.getHostEvalVars().empty() &&
212 "unexpected host_eval in target device module");
215 targetOp.getDependVarsMutable().clear();
216 targetOp.setDependKindsAttr(
nullptr);
217 targetOp.getDependIteratedMutable().clear();
218 targetOp.setDependIteratedKindsAttr(
nullptr);
219 targetOp.getDeviceMutable().clear();
220 targetOp.getDynGroupprivateSizeMutable().clear();
221 targetOp.setDynGroupprivateAccessGroupAttr(
nullptr);
222 targetOp.setDynGroupprivateFallbackAttr(
nullptr);
223 targetOp.getIfExprMutable().clear();
224 targetOp.getInReductionVarsMutable().clear();
225 targetOp.setInReductionByrefAttr(
nullptr);
226 targetOp.setInReductionSymsAttr(
nullptr);
231 for (
Value allocVar : targetOp.getAllocateVars())
233 for (
Value allocVar : targetOp.getAllocatorVars())
235 for (
Value isDevPtr : targetOp.getIsDevicePtrVars())
237 for (
Value mapVar : targetOp.getHasDeviceAddrVars())
238 collectRewrite(cast<omp::MapInfoOp>(mapVar.getDefiningOp()), mapInfos);
239 for (
Value mapVar : targetOp.getMapVars())
240 collectRewrite(cast<omp::MapInfoOp>(mapVar.getDefiningOp()), mapInfos);
241 for (
Value privateVar : targetOp.getPrivateVars())
243 for (
Value threadLimit : targetOp.getThreadLimitVars())
248 for (omp::MapInfoOp mapOp : mapInfos) {
251 if (Value varPtrPtr = mapOp.getVarPtrPtr())
255 mapOp.getBoundsMutable().clear();
256 mapOp->moveBefore(&block, block.
end());
259 builder.setInsertionPointToStart(&block);
264 llvm::SmallVector<Type> newFnArgTypes(functionType.getParams());
265 for (Value value : rewriteValues) {
267 Operation *definingOp = value.getDefiningOp();
269 rewriteValue = builder.clone(*value.getDefiningOp())->getResult(0);
271 rewriteValue = block.
addArgument(value.getType(), value.getLoc());
272 newFnArgTypes.push_back(rewriteValue.
getType());
274 value.replaceAllUsesWith(rewriteValue);
278 for (omp::TargetOp targetOp : targetOps)
279 targetOp->moveBefore(&block, block.
end());
282 builder.setInsertionPointToEnd(&block);
283 LLVM::ReturnOp::create(builder, funcOp.getLoc(),
ValueRange());
291 funcOp.setType(LLVM::LLVMFunctionType::get(
292 LLVM::LLVMVoidType::get(&
getContext()), newFnArgTypes));
static std::optional< omp::DeclareTargetDeviceType > getDeclareTargetDevice(Operation &op)
Provide the device_type of an omp.declare_target attribute, if defined.
static bool keepHostOpInDevice(Operation &op)
Some host operations, like llvm.mlir.addressof and constants, must remain in the device module becaus...
static void collectRewrite(omp::MapInfoOp mapOp, llvm::SetVector< omp::MapInfoOp > &rewrites)
Add an omp.map.info operation and all its members recursively to the output set to be later rewritten...
This class represents an argument of a Block.
Block represents an ordered list of Operations.
iterator_range< args_iterator > addArguments(TypeRange types, ArrayRef< Location > locs)
Add one argument to the argument list for each type specified in the list.
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
BlockArgListType getArguments()
Dialect * getLoadedDialect(StringRef name)
Get a registered IR dialect with the given namespace.
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Operation * cloneWithoutRegions(IRMapping &mapper)
Create a partial copy of this operation without traversing into attached regions.
operand_range getOperands()
Returns an iterator on the underlying Value's.
MLIRContext * getContext()
Return the context this operation is associated with.
void erase()
Remove this operation from its parent block and delete it.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
BlockArgListType getArguments()
unsigned getNumArguments()
ValueTypeRange< BlockArgListType > getArgumentTypes()
Returns the argument types of the first block within the region.
void takeBody(Region &other)
Takes body of another region (that region will have no body after this operation completes).
RetT walk(FnT &&callback)
Walk all nested operations, blocks or regions (including this region), depending on the type of callb...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
static WalkResult advance()
static WalkResult interrupt()
Include the generated interface declarations.
bool isPure(Operation *op)
Returns true if the given operation is pure, i.e., is speculatable that does not touch memory.