https://github.com/bcardosolopes updated https://github.com/llvm/llvm-project/pull/242111
>From 9f0686b0acee07de9e0507ff1027a0cb278ddd01 Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Lopes <[email protected]> Date: Wed, 11 Nov 2026 14:51:56 -0800 Subject: [PATCH] [CIR] Annotate AMDGPU printf ops with constant strings before lowering Resolve constant format strings while producers are still CIR, so the AMDGPU printf lowering does not meet converted producers under one-shot conversion. This is required work for the one-shot LLVM conversion flip: record one string per operand on each op and read the annotation in the lowering, instead of walking producers there. Direct constants use matchPattern and single widening steps use CastOp::fold; the cast chain walk stays hand written because CastOp is not ConstantLike and its fold folds narrowing casts. New tests cover int-array globals, stale annotations, and a narrowing cast that must not fold. Assisted-by: Muse Code (Muse). --- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 4 + .../DirectToLLVM/LowerToLLVMOffloadPrintf.cpp | 3 +- .../DirectToLLVM/LowerToLLVMOffloadPrintf.h | 10 + .../LowerToLLVMOffloadPrintfAMDGPU.cpp | 238 ++++++++--- .../CIR/Lowering/offload-printf-amdgpu.cir | 403 ++++++++++++++++++ 5 files changed, 598 insertions(+), 60 deletions(-) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 461b72839ec70..98c01696b9620 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -12,6 +12,7 @@ #include "LowerToLLVM.h" #include "LowerToLLVMMetadata.h" +#include "LowerToLLVMOffloadPrintf.h" #include "LowerToLLVMOpenCLMetadata.h" #include <array> @@ -5237,6 +5238,9 @@ void ConvertCIRToLLVMPass::runOnOperation() { // the annotations attribute is filtered out during FuncOp/GlobalOp lowering. collectGlobalAnnotations(module); + // Resolve constant printf strings while their producers are still CIR. + annotateAMDGPUPrintfStrings(module, symbolTables); + mlir::ConversionTarget target(getContext()); target.addLegalOp<mlir::ModuleOp>(); target.addLegalDialect<mlir::LLVM::LLVMDialect>(); diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintf.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintf.cpp index 7422a5d1cccfa..420c511221de3 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintf.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintf.cpp @@ -35,8 +35,7 @@ mlir::LogicalResult CIRToLLVMOffloadPrintfOpLowering::matchAndRewrite( return lowerNVPTXPrintf(op, adaptor.getFormat(), adaptor.getArgs(), rewriter, dataLayout, symbolTables); - if (triple.isAMDGCN() || - (triple.isSPIRV() && triple.getVendor() == llvm::Triple::AMD)) { + if (isAMDGPUPrintfModule(mod)) { bool isBuffered = false; if (auto kind = mod->getAttrOfType<mlir::StringAttr>( cir::CIRDialect::getAMDGPUPrintfKindAttrName())) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintf.h b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintf.h index 69a2811de9adf..a6362cd3dbbf1 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintf.h +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintf.h @@ -41,6 +41,16 @@ mlir::LogicalResult lowerAMDGPUPrintf(cir::OffloadPrintfOp op, unsigned genericAddrSpace, bool isBuffered); +/// Whether \p module lowers device printf with the AMDGPU runtime +/// interface: an AMDGCN triple, or SPIR-V with the AMD vendor. +bool isAMDGPUPrintfModule(mlir::ModuleOp module); + +/// Resolve constant format strings for AMDGPU printf ops while their +/// producers are still CIR, recording one string per operand on each +/// op for the lowering to read. Must run before ConvertCIRToLLVM. +void annotateAMDGPUPrintfStrings(mlir::ModuleOp module, + mlir::SymbolTableCollection &symbolTables); + } // namespace direct } // namespace cir diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintfAMDGPU.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintfAMDGPU.cpp index 62883b562d89f..d832ce97a399e 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintfAMDGPU.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintfAMDGPU.cpp @@ -16,9 +16,12 @@ #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/Matchers.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" +#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SparseBitVector.h" #include "llvm/Support/MathExtras.h" +#include "llvm/TargetParser/Triple.h" #include "llvm/Transforms/Utils/AMDGPUEmitPrintf.h" namespace cir { @@ -26,48 +29,118 @@ namespace direct { namespace { +// One entry per operand, format first. A string entry means the +// operand is a constant string; a unit entry means it is not. +constexpr llvm::StringLiteral kPrintfConstStringsAttrName = + "cir.printf.const_strings"; + +// Fold a constant through widening integral casts, each at its own width. +// TODO: use the regular fold helpers once they fold widening cast chains +// without also folding narrowing casts. CastOp is not ConstantLike and +// its fold folds narrowing casts, so the chain stays hand written today. +std::optional<llvm::APInt> +foldConstantInt(mlir::Value value, + llvm::SmallPtrSetImpl<mlir::Operation *> &seen) { + mlir::Operation *def = value.getDefiningOp(); + if (!def || !seen.insert(def).second) + return std::nullopt; + cir::IntAttr intAttr; + if (mlir::matchPattern(value, mlir::m_Constant(&intAttr))) + return intAttr.getValue(); + auto cast = mlir::dyn_cast<cir::CastOp>(def); + if (!cast || cast.getKind() != cir::CastKind::integral) + return std::nullopt; + auto srcTy = mlir::dyn_cast<cir::IntType>(cast.getSrc().getType()); + auto dstTy = mlir::dyn_cast<cir::IntType>(cast.getType()); + if (!srcTy || !dstTy || dstTy.getWidth() < srcTy.getWidth() || + dstTy.getWidth() > 64) + return std::nullopt; + // One widening step from a direct constant is the op's own fold; + // chains recurse, since fold sees only the immediate source. + llvm::SmallVector<mlir::OpFoldResult, 1> folded; + if (succeeded(cast->fold(folded)) && folded.size() == 1) + if (auto attr = mlir::dyn_cast<mlir::Attribute>(folded[0])) + if (auto foldedInt = mlir::dyn_cast<cir::IntAttr>(attr)) + return foldedInt.getValue(); + std::optional<llvm::APInt> foldedSrc = foldConstantInt(cast.getSrc(), seen); + if (!foldedSrc) + return std::nullopt; + return srcTy.isUnsigned() ? foldedSrc->zextOrTrunc(dstTy.getWidth()) + : foldedSrc->sextOrTrunc(dstTy.getWidth()); +} + std::optional<int64_t> getConstantInt(mlir::Value value) { - auto constOp = value.getDefiningOp<cir::ConstantOp>(); - auto intAttr = - constOp ? mlir::dyn_cast<cir::IntAttr>(constOp.getValue()) : nullptr; - if (!intAttr) + bool isUnsigned = false; + if (auto intTy = mlir::dyn_cast<cir::IntType>(value.getType())) + isUnsigned = !intTy.isSigned(); + llvm::SmallPtrSet<mlir::Operation *, 4> seen; + std::optional<llvm::APInt> result = foldConstantInt(value, seen); + if (!result || result->getBitWidth() > 64) return std::nullopt; - const llvm::APInt &v = intAttr.getValue(); - if (mlir::cast<cir::IntType>(intAttr.getType()).isSigned() - ? v.getSignificantBits() > 64 - : v.getActiveBits() > 63) + // An unsigned value past INT64_MAX cannot be a byte offset here: + // returning it would wrap negative in the signed offset below. + if (isUnsigned ? result->getActiveBits() > 63 + : result->getSignificantBits() > 64) return std::nullopt; - return v.getSExtValue(); + return isUnsigned ? result->getZExtValue() : result->getSExtValue(); } bool isByteType(mlir::Type ty) { - auto intTy = mlir::dyn_cast<cir::IntType>(ty); - return intTy && intTy.getWidth() == 8; + if (auto intTy = mlir::dyn_cast<cir::IntType>(ty)) + return intTy.getWidth() == 8; + return ty.isInteger(8); +} + +std::optional<int64_t> scaledOffset(int64_t index, mlir::Type elemTy, + const mlir::DataLayout &dataLayout) { + llvm::TypeSize size = dataLayout.getTypeAllocSize(elemTy); + if (size.isScalable() || size.getFixedValue() >= (1ULL << 63)) + return std::nullopt; + int64_t offset; + if (llvm::MulOverflow(index, static_cast<int64_t>(size.getFixedValue()), + offset)) + return std::nullopt; + return offset; } bool getConstantStringInfo(mlir::Value ptr, mlir::ModuleOp mod, + const mlir::DataLayout &dataLayout, mlir::SymbolTableCollection &symbolTables, std::string &str) { int64_t offset = 0; + llvm::SmallPtrSet<mlir::Operation *, 8> seen; while (mlir::Operation *def = ptr.getDefiningOp()) { + if (!seen.insert(def).second) + return false; if (auto cast = mlir::dyn_cast<cir::CastOp>(def)) { if (cast.getKind() != cir::CastKind::array_to_ptrdecay && cast.getKind() != cir::CastKind::bitcast && cast.getKind() != cir::CastKind::address_space) break; ptr = cast.getSrc(); - } else if (auto elt = mlir::dyn_cast<cir::GetElementOp>(def)) { - std::optional<int64_t> index = getConstantInt(elt.getIndex()); - if (!index || !isByteType(elt.getElementType()) || - llvm::AddOverflow(offset, *index, offset)) - return false; - ptr = elt.getBase(); } else if (auto stride = mlir::dyn_cast<cir::PtrStrideOp>(def)) { + mlir::Type elemTy = stride.getElementType(); + // Lowering treats void and function elements as bytes, and + // neither has a data layout entry. + if (mlir::isa<cir::VoidType, cir::FuncType>(elemTy)) + elemTy = cir::IntType::get(elemTy.getContext(), 8, false); std::optional<int64_t> index = getConstantInt(stride.getStride()); - if (!index || !isByteType(stride.getElementType()) || - llvm::AddOverflow(offset, *index, offset)) + std::optional<int64_t> step = + index ? scaledOffset(*index, elemTy, dataLayout) : std::nullopt; + if (!step || llvm::AddOverflow(offset, *step, offset)) return false; ptr = stride.getBase(); + } else if (auto elt = mlir::dyn_cast<cir::GetElementOp>(def)) { + auto arrayTy = + mlir::dyn_cast<cir::ArrayType>(elt.getBaseType().getPointee()); + std::optional<int64_t> index = getConstantInt(elt.getIndex()); + std::optional<int64_t> step = + arrayTy && index + ? scaledOffset(*index, elt.getElementType(), dataLayout) + : std::nullopt; + if (!step || llvm::AddOverflow(offset, *step, offset)) + return false; + ptr = elt.getBase(); } else { break; } @@ -76,45 +149,46 @@ bool getConstantStringInfo(mlir::Value ptr, mlir::ModuleOp mod, auto getGlobal = ptr.getDefiningOp<cir::GetGlobalOp>(); if (!getGlobal) return false; - - // The global may or may not have been converted already. - std::string data; mlir::Operation *symbol = symbolTables.lookupSymbolIn(mod, getGlobal.getNameAttr()); - if (auto global = mlir::dyn_cast_if_present<cir::GlobalOp>(symbol)) { - std::optional<mlir::Attribute> init = global.getInitialValue(); - auto arrayTy = mlir::dyn_cast<cir::ArrayType>(global.getSymType()); - if (!global.getConstant() || !init || !arrayTy || - !isByteType(arrayTy.getElementType())) - return false; - if (auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(*init)) { - auto strAttr = mlir::dyn_cast<mlir::StringAttr>(constArr.getElts()); - if (!strAttr) - return false; - data = strAttr.getValue().str(); - } else if (!mlir::isa<cir::ZeroAttr>(*init)) { - return false; - } - // Add back the trailing NULs the string attribute leaves out. - data.resize(arrayTy.getSize(), '\0'); - } else if (auto global = - mlir::dyn_cast_if_present<mlir::LLVM::GlobalOp>(symbol)) { - if (!global.getConstant()) - return false; - mlir::Attribute value = global.getValueOrNull(); - if (auto strAttr = mlir::dyn_cast_if_present<mlir::StringAttr>(value)) { + auto global = mlir::dyn_cast_if_present<cir::GlobalOp>(symbol); + if (!global) + return false; + + std::string data; + std::optional<mlir::Attribute> init = global.getInitialValue(); + auto arrayTy = mlir::dyn_cast<cir::ArrayType>(global.getSymType()); + if (!global.getConstant() || !init || !arrayTy || + !isByteType(arrayTy.getElementType())) + return false; + if (auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(*init)) { + if (auto strAttr = mlir::dyn_cast<mlir::StringAttr>(constArr.getElts())) { data = strAttr.getValue().str(); - } else if (auto dense = - mlir::dyn_cast_if_present<mlir::DenseIntElementsAttr>(value); - dense && dense.getElementType().isInteger(8)) { - for (const llvm::APInt &c : dense.getValues<llvm::APInt>()) - data.push_back(static_cast<char>(c.getZExtValue())); + } else if (auto arrayAttr = + mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts())) { + for (mlir::Attribute elt : arrayAttr) { + llvm::APInt byte; + if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(elt)) { + if (!isByteType(intAttr.getType())) + return false; + byte = intAttr.getValue(); + } else if (auto intAttr = mlir::dyn_cast<mlir::IntegerAttr>(elt)) { + if (!isByteType(intAttr.getType())) + return false; + byte = intAttr.getValue(); + } else { + return false; + } + data.push_back(static_cast<char>(byte.getZExtValue())); + } } else { return false; } - } else { + } else if (!mlir::isa<cir::ZeroAttr>(*init)) { return false; } + // Add back the trailing NULs the string attribute leaves out. + data.resize(arrayTy.getSize(), '\0'); if (offset < 0 || static_cast<uint64_t>(offset) > data.size()) return false; @@ -140,11 +214,24 @@ class AMDGPUPrintfLowering { unsigned genericAddrSpace) : op(op), rewriter(rewriter), dataLayout(dataLayout), symbolTables(symbolTables), mod(op->getParentOfType<mlir::ModuleOp>()), - genericAddrSpace(genericAddrSpace) {} + genericAddrSpace(genericAddrSpace) { + if (auto attr = + op->getAttrOfType<mlir::ArrayAttr>(kPrintfConstStringsAttrName)) + if (attr.size() == op.getArgs().size() + 1) + constStrings = attr; + } mlir::LogicalResult lower(mlir::ValueRange args, bool isBuffered); private: + std::optional<std::string> getConstString(size_t operandIndex) const { + if (!constStrings || operandIndex >= constStrings.size()) + return std::nullopt; + auto strAttr = mlir::dyn_cast<mlir::StringAttr>(constStrings[operandIndex]); + if (!strAttr) + return std::nullopt; + return strAttr.getValue().str(); + } mlir::Value getI32(uint32_t value) { return mlir::LLVM::ConstantOp::create( rewriter, op.getLoc(), rewriter.getI32Type(), llvm::APInt(32, value)); @@ -190,6 +277,7 @@ class AMDGPUPrintfLowering { mlir::SymbolTableCollection &symbolTables; mlir::ModuleOp mod; unsigned genericAddrSpace; + mlir::ArrayAttr constStrings; /// The rest of the op's block, which the expansion branches to. mlir::Block *cont = nullptr; @@ -199,6 +287,40 @@ class AMDGPUPrintfLowering { } // namespace +bool isAMDGPUPrintfModule(mlir::ModuleOp module) { + llvm::StringRef tripleStr; + if (auto tripleAttr = module->getAttrOfType<mlir::StringAttr>( + cir::CIRDialect::getTripleAttrName())) + tripleStr = tripleAttr.getValue(); + llvm::Triple triple(tripleStr); + return triple.isAMDGCN() || + (triple.isSPIRV() && triple.getVendor() == llvm::Triple::AMD); +} + +void annotateAMDGPUPrintfStrings(mlir::ModuleOp module, + mlir::SymbolTableCollection &symbolTables) { + mlir::MLIRContext *ctx = module.getContext(); + module.walk([&](cir::OffloadPrintfOp op) { + mlir::ModuleOp opModule = op->getParentOfType<mlir::ModuleOp>(); + if (!isAMDGPUPrintfModule(opModule)) + return; + mlir::DataLayout opLayout(opModule); + llvm::SmallVector<mlir::Attribute> entries; + auto resolve = [&](mlir::Value value) { + std::string str; + if (getConstantStringInfo(value, opModule, opLayout, symbolTables, str)) + entries.push_back(mlir::StringAttr::get(ctx, str)); + else + entries.push_back(mlir::UnitAttr::get(ctx)); + }; + resolve(op.getFormat()); + for (mlir::Value arg : op.getArgs()) + resolve(arg); + op->setAttr(kPrintfConstStringsAttrName, + mlir::ArrayAttr::get(ctx, entries)); + }); +} + mlir::Value AMDGPUPrintfLowering::createCall(llvm::StringRef name, mlir::Type resTy, mlir::ValueRange args) { @@ -434,14 +556,12 @@ mlir::Value AMDGPUPrintfLowering::callBufferedPrintfStart( stringContents.push_back({"", lenWithNull, nonConstStrLen, false}); } - mlir::OperandRange cirArgs = op->getOperands(); for (size_t i = 1; i < args.size(); i++) { if (isCStringArg(args, i)) { - std::string argStr; - if (getConstantStringInfo(cirArgs[i], mod, symbolTables, argStr)) { - auto alignedLen = llvm::alignTo(argStr.size() + 1, 8); - stringContents.push_back({argStr, /*realSize=*/{}, /*alignedSize=*/{}, - /*isConst=*/true}); + if (std::optional<std::string> argStr = getConstString(i)) { + auto alignedLen = llvm::alignTo(argStr->size() + 1, 8); + stringContents.push_back({*argStr, /*realSize=*/{}, + /*alignedSize=*/{}, /*isConst=*/true}); bufSize += alignedLen; } else { lenWithNull = getStrlenWithNull(args[i]); @@ -586,8 +706,10 @@ mlir::Value AMDGPUPrintfLowering::emitAMDGPUPrintfCall(mlir::ValueRange args, mlir::Value fmt = args[0]; - if (getConstantStringInfo(op.getFormat(), mod, symbolTables, fmtStr)) + if (std::optional<std::string> resolved = getConstString(0)) { + fmtStr = *resolved; llvm::locateAMDGPUPrintfCStrings(specIsCString, fmtStr); + } if (isBuffered) { llvm::SmallVector<StringData, 8> stringContents; diff --git a/clang/test/CIR/Lowering/offload-printf-amdgpu.cir b/clang/test/CIR/Lowering/offload-printf-amdgpu.cir index 89f37eecf7569..a734f7bfc658d 100644 --- a/clang/test/CIR/Lowering/offload-printf-amdgpu.cir +++ b/clang/test/CIR/Lowering/offload-printf-amdgpu.cir @@ -132,3 +132,406 @@ module attributes {cir.amdgpu_printf_kind = "buffered"} { // BUFFERED: [[P3:%.*]] = getelementptr inbounds i8, ptr addrspace(1) [[P2]], i32 8 // BUFFERED: [[ARG:%.*]] = zext i32 %1 to i64 // BUFFERED: store i64 [[ARG]], ptr addrspace(1) [[P3]], align 8 + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> +!u8i = !cir.int<u, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_unsigned_stride() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!s8i> + %2 = cir.const #cir.int<200> : !u8i + %3 = cir.ptr_stride %1, %2 : (!cir.ptr<!s8i>, !u8i) -> !cir.ptr<!s8i> + %4 = cir.const #cir.int<-195> : !s32i + %5 = cir.ptr_stride %3, %4 : (!cir.ptr<!s8i>, !s32i) -> !cir.ptr<!s8i> + %6 = cir.offload.printf(%5) : (!cir.ptr<!s8i>) -> !s32i + cir.return %6 : !s32i + } +} + +// An unsigned narrow index stays unsigned: 200 + (-195) = 5. +// Read as signed, the first stride would be -56 and the offset +// out of range, so nothing would fold. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_unsigned_stride() +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 12) +// BUFFERED: store i64 -3575341472883867058, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:ce61d5df9609024e,56789ABCDEFGHIJKLMNOPQRSTUV"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.func @buffered_late_global(%v: !s32i, %str: !cir.ptr<!s8i>) -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 7>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 7>> -> !cir.ptr<!s8i> + %2 = cir.offload.printf(%1, %v, %str) : (!cir.ptr<!s8i>, !s32i, !cir.ptr<!s8i>) -> !s32i + cir.return %2 : !s32i + } + cir.global "private" constant cir_private @".str" = #cir.const_array<"%d %s\0A" : !cir.array<!s8i x 6>, trailing_zeros> : !cir.array<!s8i x 7> +} + +// The global is defined after the function. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_late_global(i32 %0, ptr %1) +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:d3e1b03bc04075f4,%d %s\0A"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_negative_stride() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!s8i> + %2 = cir.const #cir.int<64> : !s32i + %3 = cir.ptr_stride %1, %2 : (!cir.ptr<!s8i>, !s32i) -> !cir.ptr<!s8i> + %4 = cir.const #cir.int<-56> : !s8i + %5 = cir.ptr_stride %3, %4 : (!cir.ptr<!s8i>, !s8i) -> !cir.ptr<!s8i> + %6 = cir.offload.printf(%5) : (!cir.ptr<!s8i>) -> !s32i + cir.return %6 : !s32i + } +} + +// A negative signed stride subtracts: the folded format is the string at +// byte 64 - 56 = 8. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_negative_stride() +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 12) +// BUFFERED: store i64 6552682480851187470, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:5aefcdffad0f770e,89ABCDEFGHIJKLMNOPQRSTUV"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_get_element() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %idx = cir.const #cir.int<11> : !s32i + %1 = cir.get_element %0[%idx : !s32i] : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!s8i> + %2 = cir.offload.printf(%1) : (!cir.ptr<!s8i>) -> !s32i + cir.return %2 : !s32i + } +} + +// A nonzero get_element index folds like the equivalent stride. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_get_element() +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 12) +// BUFFERED: store i64 9208594728922682447, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:7fcb801de1f8944f,BCDEFGHIJKLMNOPQRSTUV"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> +!s16i = !cir.int<s, 16> +!u16i = !cir.int<u, 16> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_zext_sext() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!s8i> + %2 = cir.const #cir.int<-56> : !s8i + %3 = cir.cast integral %2 : !s8i -> !s16i + %4 = cir.cast integral %3 : !s16i -> !u16i + %5 = cir.ptr_stride %1, %4 : (!cir.ptr<!s8i>, !u16i) -> !cir.ptr<!s8i> + %6 = cir.const #cir.int<-65463> : !s32i + %7 = cir.ptr_stride %5, %6 : (!cir.ptr<!s8i>, !s32i) -> !cir.ptr<!s8i> + %8 = cir.offload.printf(%7) : (!cir.ptr<!s8i>) -> !s32i + cir.return %8 : !s32i + } +} + +// A zext of a sext keeps the sign extended bits: -56 widens to 65480, +// and 65480 - 65463 = 17. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_zext_sext() +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 12) +// BUFFERED: store i64 -8663088140963090983, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:87c6873c1bfeedd9,HIJKLMNOPQRSTUV"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_stride_i32() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!s8i> + %2 = cir.cast bitcast %1 : !cir.ptr<!s8i> -> !cir.ptr<!s32i> + %3 = cir.const #cir.int<3> : !s32i + %4 = cir.ptr_stride %2, %3 : (!cir.ptr<!s32i>, !s32i) -> !cir.ptr<!s32i> + %5 = cir.cast bitcast %4 : !cir.ptr<!s32i> -> !cir.ptr<!s8i> + %6 = cir.offload.printf(%5) : (!cir.ptr<!s8i>) -> !s32i + cir.return %6 : !s32i + } +} + +// A stride scales by the element size: 3 elements of 4 bytes land at byte 12. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_stride_i32() +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 12) +// BUFFERED: store i64 7009764703486015180, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:6147afdbc65af6cc,CDEFGHIJKLMNOPQRSTUV"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_void_stride() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!s8i> + %2 = cir.cast bitcast %1 : !cir.ptr<!s8i> -> !cir.ptr<!cir.void> + %3 = cir.const #cir.int<4> : !s32i + %4 = cir.ptr_stride %2, %3 : (!cir.ptr<!cir.void>, !s32i) -> !cir.ptr<!cir.void> + %5 = cir.cast bitcast %4 : !cir.ptr<!cir.void> -> !cir.ptr<!s8i> + %6 = cir.offload.printf(%5) : (!cir.ptr<!s8i>) -> !s32i + cir.return %6 : !s32i + } +} + +// A stride over void steps by bytes, the way lowering treats void +// elements, so the folded format is the string at byte 4. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_void_stride() +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 12) +// BUFFERED: store i64 -3566044351531173967, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:ce82dd8e55b237b1,456789ABCDEFGHIJKLMNOPQRSTUV"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<[#cir.int<37> : !s8i, #cir.int<100> : !s8i], trailing_zeros> : !cir.array<!s8i x 3> + cir.func @buffered_int_array(%v: !s32i) -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 3>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 3>> -> !cir.ptr<!s8i> + %2 = cir.offload.printf(%1, %v) : (!cir.ptr<!s8i>, !s32i) -> !s32i + cir.return %2 : !s32i + } +} + +// A format global spelled as integer elements folds the same way as +// the string spelling. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_int_array(i32 %0) +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 20) +// BUFFERED: store i64 -2582314622382785113, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:dc29c6885a93bda7,%d"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<[#cir.int<37> : !s8i, #cir.int<100> : !s8i], trailing_zeros> : !cir.array<!s8i x 3> + cir.func @buffered_unreachable(%v: !s32i) -> !s32i { + %z = cir.const #cir.int<0> : !s32i + cir.return %z : !s32i + ^bb1: + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 3>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 3>> -> !cir.ptr<!s8i> + %2 = cir.offload.printf(%1, %v) : (!cir.ptr<!s8i>, !s32i) -> !s32i + cir.return %2 : !s32i + } +} + +// A printf in a block no branch reaches still folds: the format is +// resolved before conversion, which visits unreachable blocks too. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_unreachable(i32 %0) +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 20) +// BUFFERED: store i64 -2582314622382785113, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:dc29c6885a93bda7,%d"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".fmt" = #cir.const_array<"%d %s\0A" : !cir.array<!s8i x 6>, trailing_zeros> : !cir.array<!s8i x 7> + cir.global "private" constant cir_private @".empty" = #cir.const_array<[#cir.int<0> : !s8i], trailing_zeros> : !cir.array<!s8i x 1> + cir.func @buffered_empty_arg(%v: !s32i) -> !s32i { + %0 = cir.get_global @".fmt" : !cir.ptr<!cir.array<!s8i x 7>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 7>> -> !cir.ptr<!s8i> + %2 = cir.get_global @".empty" : !cir.ptr<!cir.array<!s8i x 1>> + %3 = cir.cast array_to_ptrdecay %2 : !cir.ptr<!cir.array<!s8i x 1>> -> !cir.ptr<!s8i> + %4 = cir.offload.printf(%1, %v, %3) : (!cir.ptr<!s8i>, !s32i, !cir.ptr<!s8i>) -> !s32i + cir.return %4 : !s32i + } +} + +// An empty constant %s argument takes the constant path: 8 aligned +// bytes for the empty string, with no runtime length computation. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_empty_arg(i32 %0) +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 28) +// BUFFERED-NOT: load i8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:d3e1b03bc04075f4,%d %s\0A"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<[#cir.int<37> : !s8i, #cir.int<100> : !s8i], trailing_zeros> : !cir.array<!s8i x 3> + cir.func @buffered_stale_attr(%v: !s32i) -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 3>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 3>> -> !cir.ptr<!s8i> + %2 = "cir.offload.printf"(%1, %v) {cir.printf.const_strings = ["WRONG", unit]} : (!cir.ptr<!s8i>, !s32i) -> !s32i + cir.return %2 : !s32i + } +} + +// A bogus annotation carried in from the input is overwritten with +// the resolved strings before lowering reads it. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_stale_attr(i32 %0) +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 20) +// BUFFERED: store i64 -2582314622382785113, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:dc29c6885a93bda7,%d"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_narrowing_cast() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!s8i> + %2 = cir.const #cir.int<261> : !s32i + %3 = cir.cast integral %2 : !s32i -> !s8i + %4 = cir.ptr_stride %1, %3 : (!cir.ptr<!s8i>, !s8i) -> !cir.ptr<!s8i> + %5 = cir.offload.printf(%4) : (!cir.ptr<!s8i>) -> !s32i + cir.return %5 : !s32i + } +} + +// A narrowing cast is not folded: 261 would truncate to 5 in i8, +// but the walk refuses narrowing casts, so the format length is +// computed at runtime and only the dummy metadata entry is emitted. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_narrowing_cast() +// BUFFERED: load i8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:ffffffff,\22Non const format string\22"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_get_element_s32() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %1 = cir.cast bitcast %0 : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!cir.array<!s32i x 8>> + %idx = cir.const #cir.int<3> : !s32i + %2 = cir.get_element %1[%idx : !s32i] : !cir.ptr<!cir.array<!s32i x 8>> -> !cir.ptr<!s32i> + %3 = cir.cast bitcast %2 : !cir.ptr<!s32i> -> !cir.ptr<!s8i> + %4 = cir.offload.printf(%3) : (!cir.ptr<!s8i>) -> !s32i + cir.return %4 : !s32i + } +} + +// A get_element index scales by the element size: 3 elements of +// 4 bytes land at byte 12, the same offset as the equivalent stride. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_get_element_s32() +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 12) +// BUFFERED: store i64 7009764703486015180, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:6147afdbc65af6cc,CDEFGHIJKLMNOPQRSTUV"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "hostcall"} { + cir.global "private" constant cir_private @".fmt" = #cir.const_array<"%s\0A" : !cir.array<!s8i x 3>, trailing_zeros> : !cir.array<!s8i x 4> + cir.func @hostcall_constant_format(%str: !cir.ptr<!s8i>) -> !s32i { + %0 = cir.get_global @".fmt" : !cir.ptr<!cir.array<!s8i x 4>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 4>> -> !cir.ptr<!s8i> + %2 = cir.offload.printf(%1, %str) : (!cir.ptr<!s8i>, !cir.ptr<!s8i>) -> !s32i + cir.return %2 : !s32i + } +} + +// The annotated format marks the argument as a string, so it is +// appended with the string runtime call instead of as a scalar. +// HOSTCALL-LABEL: define{{.*}} i32 @hostcall_constant_format(ptr %0) +// HOSTCALL: [[DESC:%.*]] = call i64 @__ockl_printf_append_string_n(i64 {{.*}}, ptr @.fmt, i64 {{.*}}, i32 0) +// HOSTCALL: call i64 @__ockl_printf_append_string_n(i64 [[DESC]], ptr %0, i64 {{.*}}, i32 1) + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_past_end() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!s8i> + %2 = cir.const #cir.int<40> : !s32i + %3 = cir.ptr_stride %1, %2 : (!cir.ptr<!s8i>, !s32i) -> !cir.ptr<!s8i> + %4 = cir.offload.printf(%3) : (!cir.ptr<!s8i>) -> !s32i + cir.return %4 : !s32i + } +} + +// An offset past the end of the string is rejected, so the format +// length is computed at runtime like the narrowing case. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_past_end() +// BUFFERED: load i8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:ffffffff,\22Non const format string\22"} + +// ----- + +!s32i = !cir.int<s, 32> +!s8i = !cir.int<s, 8> + +module attributes {cir.amdgpu_printf_kind = "buffered"} { + cir.global "private" constant cir_private @".str" = #cir.const_array<"0123456789ABCDEFGHIJKLMNOPQRSTUV" : !cir.array<!s8i x 32>, trailing_zeros> : !cir.array<!s8i x 33> + cir.func @buffered_identity_cast() -> !s32i { + %0 = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 33>> + %1 = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s8i x 33>> -> !cir.ptr<!s8i> + %2 = cir.const #cir.int<7> : !s32i + %3 = cir.cast integral %2 : !s32i -> !s32i + %4 = cir.ptr_stride %1, %3 : (!cir.ptr<!s8i>, !s32i) -> !cir.ptr<!s8i> + %5 = cir.offload.printf(%4) : (!cir.ptr<!s8i>) -> !s32i + cir.return %5 : !s32i + } +} + +// A same-type cast folds to its constant, landing at byte 7. +// BUFFERED-LABEL: define{{.*}} i32 @buffered_identity_cast() +// BUFFERED: call ptr addrspace(1) @__printf_alloc(i32 12) +// BUFFERED: store i64 2513007642441926819, ptr addrspace(1) %{{.*}}, align 8 +// BUFFERED: !llvm.printf.fmts = !{[[FMT:![0-9]+]]} +// BUFFERED: [[FMT]] = !{!"0:0:22dfff22e597c8a3,789ABCDEFGHIJKLMNOPQRSTUV"} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
