| 1 | //===- ACCExecutableDirectivePatterns.cpp - ACC exec patterns ---*- 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 | // Lowers OpenACC executable directives (init, shutdown, wait, set) to calls to |
| 10 | // an OpenACC offloading runtime compiler interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Conversion/OpenACCToLLVM/ACCToLLVM.h" |
| 15 | #include "mlir/Conversion/OpenACCToLLVM/ACCToLLVMUtils.h" |
| 16 | |
| 17 | #include "mlir/Conversion/LLVMCommon/Pattern.h" |
| 18 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 19 | #include "mlir/Dialect/LLVMIR/LLVMConstantOps.h" |
| 20 | #include "mlir/Dialect/OpenACC/OpenACC.h" |
| 21 | #include "mlir/IR/PatternMatch.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
| 23 | |
| 24 | #include <cstdint> |
| 25 | |
| 26 | using namespace mlir; |
| 27 | using namespace mlir::acc; |
| 28 | |
| 29 | namespace { |
| 30 | static Value castToI32(Location loc, Value value, bool isUnsigned, |
| 31 | ConversionPatternRewriter &rewriter) { |
| 32 | Type i32Ty = rewriter.getI32Type(); |
| 33 | unsigned bitwidth = value.getType().getIntOrFloatBitWidth(); |
| 34 | if (bitwidth > 32) |
| 35 | return arith::TruncIOp::create(builder&: rewriter, location: loc, out: i32Ty, in: value); |
| 36 | if (bitwidth < 32) { |
| 37 | if (isUnsigned) |
| 38 | return arith::ExtUIOp::create(builder&: rewriter, location: loc, out: i32Ty, in: value); |
| 39 | return arith::ExtSIOp::create(builder&: rewriter, location: loc, out: i32Ty, in: value); |
| 40 | } |
| 41 | return value; |
| 42 | } |
| 43 | |
| 44 | template <typename OpTy> |
| 45 | struct ACCExecutableDirectivePattern : public ConvertOpToLLVMPattern<OpTy> { |
| 46 | ACCExecutableDirectivePattern(const LLVMTypeConverter &converter, |
| 47 | Region &globalSymbolRegion, |
| 48 | SymbolTable &symbolTable, |
| 49 | const ACCRuntimeCallConfig &config, |
| 50 | PatternBenefit benefit = 1) |
| 51 | : ConvertOpToLLVMPattern<OpTy>(converter, benefit), |
| 52 | globalSymbolRegion(globalSymbolRegion), symbolTable(symbolTable), |
| 53 | config(config) {} |
| 54 | |
| 55 | Region &globalSymbolRegion; |
| 56 | SymbolTable &symbolTable; |
| 57 | ACCRuntimeCallConfig config; |
| 58 | }; |
| 59 | |
| 60 | struct WaitOpLowering : public ACCExecutableDirectivePattern<WaitOp> { |
| 61 | using ACCExecutableDirectivePattern<WaitOp>::ACCExecutableDirectivePattern; |
| 62 | |
| 63 | LogicalResult |
| 64 | matchAndRewrite(WaitOp op, WaitOp::Adaptor adaptor, |
| 65 | ConversionPatternRewriter &rewriter) const override { |
| 66 | Location loc = op->getLoc(); |
| 67 | |
| 68 | auto emitWait = [&]() -> LogicalResult { |
| 69 | Value asyncOperand = op.getAsyncOperand() |
| 70 | ? rewriter.getRemappedValue(key: op.getAsyncOperand()) |
| 71 | : Value(); |
| 72 | Value asyncQueue = |
| 73 | getAsyncQueue(loc, asyncOperand, asyncOnly: op.getAsync(), builder&: rewriter, config); |
| 74 | SmallVector<Value> waitValues; |
| 75 | for (Value operand : op.getWaitOperands()) |
| 76 | waitValues.push_back(Elt: rewriter.getRemappedValue(key: operand)); |
| 77 | |
| 78 | Value deviceNum = adaptor.getWaitDevnum(); |
| 79 | if (deviceNum) { |
| 80 | // The converted operand is signless, so inspect the original type. |
| 81 | bool isUnsigned = op.getWaitDevnum().getType().isUnsignedInteger(); |
| 82 | deviceNum = castToI32(loc, value: deviceNum, isUnsigned, rewriter); |
| 83 | } |
| 84 | |
| 85 | return emitWaitCall(loc, waitOperands: waitValues, asyncQueue, builder&: rewriter, |
| 86 | globalSymbolRegion, symbolTable, config, deviceNum); |
| 87 | }; |
| 88 | |
| 89 | if (failed(Result: emitGuardedByIfCond(loc, ifCond: op.getIfCond(), rewriter, emitFn: emitWait))) |
| 90 | return failure(); |
| 91 | |
| 92 | rewriter.eraseOp(op); |
| 93 | return success(); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | /// Emit a call to a runtime entry point taking |
| 98 | /// `(ident, flags, deviceType, deviceNum)`. A null `deviceNum` selects the |
| 99 | /// current device. |
| 100 | static LogicalResult |
| 101 | emitDeviceOperationCall(Location loc, RuntimeFunction fn, DeviceType deviceType, |
| 102 | Value deviceNum, StringRef functionName, |
| 103 | Region &globalSymbolRegion, SymbolTable &symbolTable, |
| 104 | ConversionPatternRewriter &rewriter, |
| 105 | const ACCRuntimeCallConfig &config) { |
| 106 | Type i64Ty = rewriter.getI64Type(); |
| 107 | Value deviceTypeValue = LLVM::ConstantOp::create( |
| 108 | builder&: rewriter, location: loc, type: i64Ty, value: config.getDeviceTypeRuntimeValue(type: deviceType)); |
| 109 | Value ident = createIdent(loc, functionName, builder&: rewriter, globalSymbolRegion, |
| 110 | config, symbolTable: &symbolTable); |
| 111 | Value flags = LLVM::ConstantOp::create(builder&: rewriter, location: loc, type: i64Ty, value: 0); |
| 112 | Value deviceNumValue = |
| 113 | deviceNum ? castToI64(loc, value: deviceNum, builder&: rewriter) |
| 114 | : LLVM::ConstantOp::create(builder&: rewriter, location: loc, type: i64Ty, value: -1); |
| 115 | return createRuntimeCall(loc, builder&: rewriter, globalSymbolRegion, symbolTable, fn, |
| 116 | config, |
| 117 | arguments: {ident, flags, deviceTypeValue, deviceNumValue}); |
| 118 | } |
| 119 | |
| 120 | static LogicalResult rewriteInitOrShutdown(Operation *op, Value deviceNum, |
| 121 | ArrayAttr deviceTypesAttr, |
| 122 | Value ifCond, bool isInit, |
| 123 | ConversionPatternRewriter &rewriter, |
| 124 | Region &globalSymbolRegion, |
| 125 | SymbolTable &symbolTable, |
| 126 | const ACCRuntimeCallConfig &config) { |
| 127 | Location loc = op->getLoc(); |
| 128 | |
| 129 | auto emitCalls = [&]() -> LogicalResult { |
| 130 | StringRef functionName = deviceNum ? getParentFunctionName(value: deviceNum) |
| 131 | : getParentFunctionName(op); |
| 132 | RuntimeFunction fn = isInit ? RuntimeFunction::ACCRTL_tgt_acc_init |
| 133 | : RuntimeFunction::ACCRTL_tgt_acc_shutdown; |
| 134 | |
| 135 | auto emitOne = [&](DeviceType deviceType) { |
| 136 | return emitDeviceOperationCall(loc, fn, deviceType, deviceNum, |
| 137 | functionName, globalSymbolRegion, |
| 138 | symbolTable, rewriter, config); |
| 139 | }; |
| 140 | |
| 141 | if (!deviceTypesAttr) |
| 142 | return emitOne(DeviceType::None); |
| 143 | |
| 144 | for (Attribute attr : deviceTypesAttr) { |
| 145 | if (auto typeAttr = dyn_cast<DeviceTypeAttr>(Val&: attr)) |
| 146 | if (failed(Result: emitOne(typeAttr.getValue()))) |
| 147 | return failure(); |
| 148 | } |
| 149 | return success(); |
| 150 | }; |
| 151 | |
| 152 | if (failed(Result: emitGuardedByIfCond(loc, ifCond, rewriter, emitFn: emitCalls))) |
| 153 | return failure(); |
| 154 | |
| 155 | rewriter.eraseOp(op); |
| 156 | return success(); |
| 157 | } |
| 158 | |
| 159 | struct InitOpLowering : public ACCExecutableDirectivePattern<InitOp> { |
| 160 | using ACCExecutableDirectivePattern<InitOp>::ACCExecutableDirectivePattern; |
| 161 | |
| 162 | LogicalResult |
| 163 | matchAndRewrite(InitOp op, InitOp::Adaptor adaptor, |
| 164 | ConversionPatternRewriter &rewriter) const override { |
| 165 | return rewriteInitOrShutdown( |
| 166 | op, deviceNum: adaptor.getDeviceNum(), deviceTypesAttr: op.getDeviceTypesAttr(), ifCond: op.getIfCond(), |
| 167 | /*isInit=*/true, rewriter, globalSymbolRegion, symbolTable, config); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | struct ShutdownOpLowering : public ACCExecutableDirectivePattern<ShutdownOp> { |
| 172 | using ACCExecutableDirectivePattern< |
| 173 | ShutdownOp>::ACCExecutableDirectivePattern; |
| 174 | |
| 175 | LogicalResult |
| 176 | matchAndRewrite(ShutdownOp op, ShutdownOp::Adaptor adaptor, |
| 177 | ConversionPatternRewriter &rewriter) const override { |
| 178 | return rewriteInitOrShutdown( |
| 179 | op, deviceNum: adaptor.getDeviceNum(), deviceTypesAttr: op.getDeviceTypesAttr(), ifCond: op.getIfCond(), |
| 180 | /*isInit=*/false, rewriter, globalSymbolRegion, symbolTable, config); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | struct SetOpLowering : public ACCExecutableDirectivePattern<SetOp> { |
| 185 | using ACCExecutableDirectivePattern<SetOp>::ACCExecutableDirectivePattern; |
| 186 | |
| 187 | LogicalResult |
| 188 | matchAndRewrite(SetOp op, SetOp::Adaptor adaptor, |
| 189 | ConversionPatternRewriter &rewriter) const override { |
| 190 | Location loc = op.getLoc(); |
| 191 | Type i64Ty = rewriter.getI64Type(); |
| 192 | |
| 193 | auto emitSet = [&]() -> LogicalResult { |
| 194 | if (Value asyncValue = adaptor.getDefaultAsync()) { |
| 195 | asyncValue = castToI64(loc, value: asyncValue, builder&: rewriter); |
| 196 | Value ident = |
| 197 | createIdent(loc, functionName: getParentFunctionName(value: asyncValue), builder&: rewriter, |
| 198 | globalSymbolRegion, config, symbolTable: &symbolTable); |
| 199 | if (failed(Result: createRuntimeCall( |
| 200 | loc, builder&: rewriter, globalSymbolRegion, symbolTable, |
| 201 | fn: RuntimeFunction::ACCRTL_tgt_acc_set_default_async, config, |
| 202 | arguments: {ident, asyncValue}))) |
| 203 | return failure(); |
| 204 | } |
| 205 | |
| 206 | if (op.getDeviceNum()) { |
| 207 | Value deviceNum = adaptor.getDeviceNum(); |
| 208 | DeviceType deviceType = DeviceType::None; |
| 209 | if (auto deviceTypeAttr = op.getDeviceTypeAttr()) |
| 210 | deviceType = deviceTypeAttr.getValue(); |
| 211 | return emitDeviceOperationCall( |
| 212 | loc, fn: RuntimeFunction::ACCRTL_tgt_acc_set_device_num, deviceType, |
| 213 | deviceNum, functionName: getParentFunctionName(value: deviceNum), globalSymbolRegion, |
| 214 | symbolTable, rewriter, config); |
| 215 | } |
| 216 | if (auto deviceTypeAttr = op.getDeviceTypeAttr()) { |
| 217 | Value deviceTypeValue = LLVM::ConstantOp::create( |
| 218 | builder&: rewriter, location: loc, type: i64Ty, |
| 219 | value: config.getDeviceTypeRuntimeValue(type: deviceTypeAttr.getValue())); |
| 220 | Value ident = createIdent(loc, functionName: StringRef(), builder&: rewriter, |
| 221 | globalSymbolRegion, config, symbolTable: &symbolTable); |
| 222 | Value flags = LLVM::ConstantOp::create(builder&: rewriter, location: loc, type: i64Ty, value: 0); |
| 223 | return createRuntimeCall( |
| 224 | loc, builder&: rewriter, globalSymbolRegion, symbolTable, |
| 225 | fn: RuntimeFunction::ACCRTL_tgt_acc_set_device_type, config, |
| 226 | arguments: {ident, flags, deviceTypeValue}); |
| 227 | } |
| 228 | return success(); |
| 229 | }; |
| 230 | |
| 231 | if (failed(Result: emitGuardedByIfCond(loc, ifCond: op.getIfCond(), rewriter, emitFn: emitSet))) |
| 232 | return failure(); |
| 233 | |
| 234 | rewriter.eraseOp(op); |
| 235 | return success(); |
| 236 | } |
| 237 | }; |
| 238 | |
| 239 | } // namespace |
| 240 | |
| 241 | void mlir::configureACCExecutableDirectiveConversionLegality( |
| 242 | ConversionTarget &target) { |
| 243 | target.addIllegalOp<acc::InitOp, acc::ShutdownOp, acc::WaitOp, acc::SetOp>(); |
| 244 | } |
| 245 | |
| 246 | void mlir::populateACCExecutableDirectivePatterns( |
| 247 | LLVMTypeConverter &converter, RewritePatternSet &patterns, |
| 248 | Region &globalSymbolRegion, SymbolTable &symbolTable, |
| 249 | const acc::ACCRuntimeCallConfig &config) { |
| 250 | patterns |
| 251 | .add<WaitOpLowering, InitOpLowering, ShutdownOpLowering, SetOpLowering>( |
| 252 | arg&: converter, args&: globalSymbolRegion, args&: symbolTable, args: config); |
| 253 | } |
| 254 | |