| 1 | //===- ACCHostDataPatterns.cpp - ACC host_data to LLVM ---------*- C++ -*-===// |
| 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 | // Lowering of the OpenACC host_data construct to the OpenACC runtime. Each |
| 10 | // use_device clause of the construct becomes the call that asks the runtime |
| 11 | // for the device address its object is mapped to, so that the body of the |
| 12 | // construct works on device addresses. An if clause on the construct decides |
| 13 | // between that address and the host one, and the body is left in place of the |
| 14 | // construct. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "mlir/Conversion/OpenACCToLLVM/ACCToLLVM.h" |
| 19 | #include "mlir/Conversion/OpenACCToLLVM/ACCToLLVMUtils.h" |
| 20 | |
| 21 | #include "mlir/Conversion/LLVMCommon/Pattern.h" |
| 22 | #include "mlir/Dialect/OpenACC/OpenACC.h" |
| 23 | #include "mlir/IR/PatternMatch.h" |
| 24 | |
| 25 | using namespace mlir; |
| 26 | using namespace mlir::acc; |
| 27 | |
| 28 | /// Returns the host_data construct \p op gives a device address to, or null |
| 29 | /// when the clause belongs to no such construct. |
| 30 | static HostDataOp getHostDataConstruct(UseDeviceOp op) { |
| 31 | for (Operation *user : op->getUsers()) |
| 32 | if (auto hostData = dyn_cast<HostDataOp>(Val: user)) |
| 33 | return hostData; |
| 34 | return nullptr; |
| 35 | } |
| 36 | |
| 37 | namespace { |
| 38 | /// A use_device clause stands for the device address of the object it names, |
| 39 | /// so it becomes the call that asks the runtime for that address. The call is |
| 40 | /// emitted before the construct, so that every use of the clause in its body |
| 41 | /// is reached by it. |
| 42 | struct UseDeviceOpLowering : public ConvertOpToLLVMPattern<UseDeviceOp> { |
| 43 | UseDeviceOpLowering(const LLVMTypeConverter &converter, |
| 44 | Region &globalSymbolRegion, SymbolTable &symbolTable, |
| 45 | const ACCRuntimeCallConfig &config, |
| 46 | PatternBenefit benefit = 1) |
| 47 | : ConvertOpToLLVMPattern<UseDeviceOp>(converter, benefit), |
| 48 | globalSymbolRegion(globalSymbolRegion), symbolTable(symbolTable), |
| 49 | config(config) {} |
| 50 | |
| 51 | LogicalResult |
| 52 | matchAndRewrite(UseDeviceOp op, OpAdaptor adaptor, |
| 53 | ConversionPatternRewriter &rewriter) const override { |
| 54 | HostDataOp hostData = getHostDataConstruct(op); |
| 55 | if (!hostData) |
| 56 | return rewriter.notifyMatchFailure( |
| 57 | arg&: op, msg: "use_device clause of no host_data construct" ); |
| 58 | |
| 59 | Location loc = op.getLoc(); |
| 60 | Value hostPtr = adaptor.getVar(); |
| 61 | auto emitCall = [&]() -> FailureOr<Value> { |
| 62 | return acc::emitGetDevicePtrCall(clauseOp: op, hostPtr, ifPresent: hostData.getIfPresent(), |
| 63 | builder&: rewriter, globalSymbolRegion, |
| 64 | symbolTable, config); |
| 65 | }; |
| 66 | // An if clause that does not hold leaves the body on host addresses. |
| 67 | auto keepHostPtr = [&]() -> FailureOr<Value> { return hostPtr; }; |
| 68 | |
| 69 | Value ifCond = hostData.getIfCond() |
| 70 | ? rewriter.getRemappedValue(key: hostData.getIfCond()) |
| 71 | : Value(); |
| 72 | if (ifCond) |
| 73 | rewriter.setInsertionPoint(hostData); |
| 74 | FailureOr<Value> devicePtr = acc::emitValueSelectedByIfCond( |
| 75 | loc, ifCond, rewriter, thenFn: emitCall, elseFn: keepHostPtr); |
| 76 | if (failed(Result: devicePtr)) |
| 77 | return failure(); |
| 78 | |
| 79 | rewriter.replaceOp(op, newValues: *devicePtr); |
| 80 | return success(); |
| 81 | } |
| 82 | |
| 83 | Region &globalSymbolRegion; |
| 84 | SymbolTable &symbolTable; |
| 85 | ACCRuntimeCallConfig config; |
| 86 | }; |
| 87 | |
| 88 | /// The construct itself has nothing left to do once its clauses hold device |
| 89 | /// addresses, so its body takes its place. |
| 90 | struct HostDataOpLowering : public ConvertOpToLLVMPattern<HostDataOp> { |
| 91 | using ConvertOpToLLVMPattern<HostDataOp>::ConvertOpToLLVMPattern; |
| 92 | |
| 93 | LogicalResult |
| 94 | matchAndRewrite(HostDataOp op, OpAdaptor, |
| 95 | ConversionPatternRewriter &rewriter) const override { |
| 96 | acc::spliceConstructRegion(op, region&: op.getRegion(), rewriter); |
| 97 | rewriter.eraseOp(op); |
| 98 | return success(); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | } // namespace |
| 103 | |
| 104 | void mlir::configureACCHostDataConversionLegality(ConversionTarget &target) { |
| 105 | target.addIllegalOp<acc::HostDataOp, acc::UseDeviceOp>(); |
| 106 | } |
| 107 | |
| 108 | void mlir::populateACCHostDataPatterns( |
| 109 | LLVMTypeConverter &converter, RewritePatternSet &patterns, |
| 110 | Region &globalSymbolRegion, SymbolTable &symbolTable, |
| 111 | const acc::ACCRuntimeCallConfig &config) { |
| 112 | patterns.add<UseDeviceOpLowering>(arg&: converter, args&: globalSymbolRegion, args&: symbolTable, |
| 113 | args: config); |
| 114 | patterns.add<HostDataOpLowering>(arg&: converter); |
| 115 | } |
| 116 | |