| 1 | //===- LowerToLLVMOffloadPrintf.cpp - Lower cir.offload.printf ------------===// |
| 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 lowers cir.offload.printf to the printf runtime interface of each |
| 10 | // device target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "LowerToLLVMOffloadPrintf.h" |
| 15 | #include "LowerToLLVM.h" |
| 16 | #include "TargetLoweringInfo.h" |
| 17 | |
| 18 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 19 | #include "llvm/TargetParser/Triple.h" |
| 20 | |
| 21 | namespace cir { |
| 22 | namespace direct { |
| 23 | |
| 24 | mlir::LogicalResult CIRToLLVMOffloadPrintfOpLowering::matchAndRewrite( |
| 25 | cir::OffloadPrintfOp op, OpAdaptor adaptor, |
| 26 | mlir::ConversionPatternRewriter &rewriter) const { |
| 27 | auto mod = op->getParentOfType<mlir::ModuleOp>(); |
| 28 | llvm::StringRef tripleStr; |
| 29 | if (auto tripleAttr = mod->getAttrOfType<mlir::StringAttr>( |
| 30 | name: cir::CIRDialect::getTripleAttrName())) |
| 31 | tripleStr = tripleAttr.getValue(); |
| 32 | llvm::Triple triple(tripleStr); |
| 33 | |
| 34 | if (triple.isNVPTX()) |
| 35 | return lowerNVPTXPrintf(op, format: adaptor.getFormat(), args: adaptor.getArgs(), |
| 36 | rewriter, dataLayout, symbolTables); |
| 37 | |
| 38 | if (isAMDGPUPrintfModule(module: mod)) { |
| 39 | bool isBuffered = false; |
| 40 | if (auto kind = mod->getAttrOfType<mlir::StringAttr>( |
| 41 | name: cir::CIRDialect::getAMDGPUPrintfKindAttrName())) |
| 42 | isBuffered = kind.getValue() == "buffered" ; |
| 43 | std::unique_ptr<cir::TargetLoweringInfo> targetInfo = |
| 44 | triple.isSPIRV() ? cir::createSPIRVTargetLoweringInfo() |
| 45 | : cir::createAMDGPUTargetLoweringInfo(); |
| 46 | unsigned genericAddrSpace = targetInfo->getTargetAddrSpaceFromCIRAddrSpace( |
| 47 | addrSpace: cir::LangAddressSpace::OffloadGeneric); |
| 48 | return lowerAMDGPUPrintf(op, operands: adaptor.getOperands(), rewriter, dataLayout, |
| 49 | symbolTables, genericAddrSpace, isBuffered); |
| 50 | } |
| 51 | |
| 52 | return op.emitError() << "offload printf lowering is NYI for target '" |
| 53 | << tripleStr << "'" ; |
| 54 | } |
| 55 | |
| 56 | } // namespace direct |
| 57 | } // namespace cir |
| 58 | |