|
| 1 | +//===- AIRSymmetricAllocToMgpuPass.cpp -------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Copyright (C) 2026, Advanced Micro Devices, Inc. All rights reserved. |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +// |
| 6 | +//===-----------------------------------------------------------------------===// |
| 7 | +// |
| 8 | +// Lower memref.alloc carrying the `air.symmetric` attribute to a call to the |
| 9 | +// runtime function `mgpuSymmetricAlloc`. The returned `!llvm.ptr` is wrapped |
| 10 | +// in an LLVM memref descriptor (struct) and projected back to the original |
| 11 | +// memref type via `builtin.unrealized_conversion_cast` so that downstream |
| 12 | +// uses keep working. |
| 13 | +// |
| 14 | +// `memref.dealloc` ops whose operand traces (through a single |
| 15 | +// `unrealized_conversion_cast`) back to a symmetric alloc are rewritten to |
| 16 | +// `mgpuSymmetricFree`. |
| 17 | +// |
| 18 | +//===-----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +#include "air/Conversion/AIRSymmetricAllocToMgpuPass.h" |
| 21 | +#include "air/Conversion/GPUPassDetail.h" |
| 22 | + |
| 23 | +#include "mlir/Dialect/Arith/IR/Arith.h" |
| 24 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 25 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 26 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 27 | +#include "mlir/IR/Builders.h" |
| 28 | +#include "mlir/Pass/Pass.h" |
| 29 | + |
| 30 | +using namespace mlir; |
| 31 | +using namespace xilinx; |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +// Ensure a private extern func declaration exists at module scope. |
| 36 | +static func::FuncOp ensureExternFunc(ModuleOp module, OpBuilder &builder, |
| 37 | + StringRef name, FunctionType type) { |
| 38 | + if (auto fn = module.lookupSymbol<func::FuncOp>(name)) |
| 39 | + return fn; |
| 40 | + OpBuilder::InsertionGuard guard(builder); |
| 41 | + builder.setInsertionPointToStart(module.getBody()); |
| 42 | + auto fn = func::FuncOp::create(builder, module.getLoc(), name, type); |
| 43 | + fn.setPrivate(); |
| 44 | + return fn; |
| 45 | +} |
| 46 | + |
| 47 | +// Compute the byte size of a static-shaped memref as an i64 SSA value. |
| 48 | +// Returns nullptr if the memref is dynamically shaped. |
| 49 | +static Value computeMemrefByteSize(OpBuilder &b, Location loc, MemRefType ty) { |
| 50 | + if (!ty.hasStaticShape()) |
| 51 | + return nullptr; |
| 52 | + int64_t numElts = 1; |
| 53 | + for (int64_t d : ty.getShape()) |
| 54 | + numElts *= d; |
| 55 | + unsigned eltBits = ty.getElementType().getIntOrFloatBitWidth(); |
| 56 | + if (eltBits == 0 || (eltBits % 8) != 0) |
| 57 | + return nullptr; |
| 58 | + int64_t totalBytes = numElts * (eltBits / 8); |
| 59 | + return arith::ConstantOp::create(b, loc, b.getI64Type(), |
| 60 | + b.getI64IntegerAttr(totalBytes)); |
| 61 | +} |
| 62 | + |
| 63 | +// Build an LLVM memref descriptor struct populated with the given pointer. |
| 64 | +// For now we support only static-shape, contiguous, identity-layout memrefs |
| 65 | +// without an offset. For dimensions: sizes from the type, strides as |
| 66 | +// row-major (innermost stride = 1). |
| 67 | +static Value buildMemrefDescriptor(OpBuilder &b, Location loc, |
| 68 | + MemRefType memrefTy, Value ptr) { |
| 69 | + ArrayRef<int64_t> shape = memrefTy.getShape(); |
| 70 | + unsigned rank = shape.size(); |
| 71 | + auto i64Ty = b.getI64Type(); |
| 72 | + auto ptrTy = LLVM::LLVMPointerType::get(b.getContext()); |
| 73 | + |
| 74 | + // Build the descriptor type: !llvm.struct<(ptr, ptr, i64, array<R x i64>, |
| 75 | + // array<R x i64>)>. For rank-0 memrefs, MLIR omits the size/stride arrays. |
| 76 | + SmallVector<Type, 5> descFields; |
| 77 | + descFields.push_back(ptrTy); |
| 78 | + descFields.push_back(ptrTy); |
| 79 | + descFields.push_back(i64Ty); |
| 80 | + if (rank > 0) { |
| 81 | + descFields.push_back(LLVM::LLVMArrayType::get(i64Ty, rank)); |
| 82 | + descFields.push_back(LLVM::LLVMArrayType::get(i64Ty, rank)); |
| 83 | + } |
| 84 | + auto structTy = LLVM::LLVMStructType::getLiteral(b.getContext(), descFields); |
| 85 | + |
| 86 | + Value desc = LLVM::PoisonOp::create(b, loc, structTy); |
| 87 | + desc = LLVM::InsertValueOp::create(b, loc, desc, ptr, ArrayRef<int64_t>{0}); |
| 88 | + desc = LLVM::InsertValueOp::create(b, loc, desc, ptr, ArrayRef<int64_t>{1}); |
| 89 | + Value zero = LLVM::ConstantOp::create(b, loc, i64Ty, b.getI64IntegerAttr(0)); |
| 90 | + desc = LLVM::InsertValueOp::create(b, loc, desc, zero, ArrayRef<int64_t>{2}); |
| 91 | + |
| 92 | + if (rank > 0) { |
| 93 | + // Compute row-major strides from shape (innermost = 1). |
| 94 | + SmallVector<int64_t> strides(rank, 1); |
| 95 | + for (int i = static_cast<int>(rank) - 2; i >= 0; --i) |
| 96 | + strides[i] = strides[i + 1] * shape[i + 1]; |
| 97 | + for (unsigned i = 0; i < rank; ++i) { |
| 98 | + Value sz = LLVM::ConstantOp::create(b, loc, i64Ty, |
| 99 | + b.getI64IntegerAttr(shape[i])); |
| 100 | + desc = LLVM::InsertValueOp::create(b, loc, desc, sz, |
| 101 | + ArrayRef<int64_t>{3, (int64_t)i}); |
| 102 | + Value st = LLVM::ConstantOp::create(b, loc, i64Ty, |
| 103 | + b.getI64IntegerAttr(strides[i])); |
| 104 | + desc = LLVM::InsertValueOp::create(b, loc, desc, st, |
| 105 | + ArrayRef<int64_t>{4, (int64_t)i}); |
| 106 | + } |
| 107 | + } |
| 108 | + return desc; |
| 109 | +} |
| 110 | + |
| 111 | +struct AIRSymmetricAllocToMgpuPass |
| 112 | + : public xilinx::air::impl::AIRSymmetricAllocToMgpuBase< |
| 113 | + AIRSymmetricAllocToMgpuPass> { |
| 114 | + |
| 115 | + AIRSymmetricAllocToMgpuPass() = default; |
| 116 | + AIRSymmetricAllocToMgpuPass(const AIRSymmetricAllocToMgpuPass &) {} |
| 117 | + |
| 118 | + void runOnOperation() override { |
| 119 | + auto module = getOperation(); |
| 120 | + OpBuilder builder(module.getContext()); |
| 121 | + auto i64Ty = builder.getI64Type(); |
| 122 | + auto ptrTy = LLVM::LLVMPointerType::get(module.getContext()); |
| 123 | + |
| 124 | + // Collect symmetric allocs. |
| 125 | + SmallVector<memref::AllocOp> symAllocs; |
| 126 | + module.walk([&](memref::AllocOp op) { |
| 127 | + if (op->hasAttr("air.symmetric")) |
| 128 | + symAllocs.push_back(op); |
| 129 | + }); |
| 130 | + |
| 131 | + if (symAllocs.empty()) |
| 132 | + return; |
| 133 | + |
| 134 | + auto allocFn = ensureExternFunc( |
| 135 | + module, builder, "mgpuSymmetricAlloc", |
| 136 | + builder.getFunctionType({i64Ty, ptrTy}, {ptrTy})); |
| 137 | + auto freeFn = ensureExternFunc( |
| 138 | + module, builder, "mgpuSymmetricFree", |
| 139 | + builder.getFunctionType({ptrTy, ptrTy}, {})); |
| 140 | + |
| 141 | + // Track the !llvm.ptr backing each lowered memref so deallocs can look |
| 142 | + // them up. |
| 143 | + DenseMap<Value, Value> symmetricMemrefToPtr; |
| 144 | + |
| 145 | + for (memref::AllocOp alloc : symAllocs) { |
| 146 | + auto memrefTy = alloc.getType(); |
| 147 | + Location loc = alloc.getLoc(); |
| 148 | + builder.setInsertionPoint(alloc); |
| 149 | + |
| 150 | + Value sizeBytes = computeMemrefByteSize(builder, loc, memrefTy); |
| 151 | + if (!sizeBytes) { |
| 152 | + alloc.emitOpError( |
| 153 | + "air.symmetric memref.alloc requires a static-shape memref with " |
| 154 | + "byte-aligned element type"); |
| 155 | + signalPassFailure(); |
| 156 | + return; |
| 157 | + } |
| 158 | + Value nullPtr = LLVM::ZeroOp::create(builder, loc, ptrTy); |
| 159 | + Value ptr = func::CallOp::create(builder, loc, allocFn, |
| 160 | + ValueRange{sizeBytes, nullPtr}) |
| 161 | + .getResult(0); |
| 162 | + |
| 163 | + Value desc = buildMemrefDescriptor(builder, loc, memrefTy, ptr); |
| 164 | + Value newMemref = UnrealizedConversionCastOp::create( |
| 165 | + builder, loc, TypeRange{memrefTy}, ValueRange{desc}) |
| 166 | + .getResult(0); |
| 167 | + symmetricMemrefToPtr[newMemref] = ptr; |
| 168 | + alloc.getResult().replaceAllUsesWith(newMemref); |
| 169 | + alloc.erase(); |
| 170 | + } |
| 171 | + |
| 172 | + // Lower deallocs whose operand traces back to a symmetric alloc. |
| 173 | + SmallVector<memref::DeallocOp> deallocs; |
| 174 | + module.walk([&](memref::DeallocOp op) { deallocs.push_back(op); }); |
| 175 | + for (memref::DeallocOp d : deallocs) { |
| 176 | + Value src = d.getMemref(); |
| 177 | + auto it = symmetricMemrefToPtr.find(src); |
| 178 | + if (it == symmetricMemrefToPtr.end()) |
| 179 | + continue; // not a symmetric memref |
| 180 | + builder.setInsertionPoint(d); |
| 181 | + Value nullPtr = LLVM::ZeroOp::create(builder, d.getLoc(), ptrTy); |
| 182 | + func::CallOp::create(builder, d.getLoc(), freeFn, |
| 183 | + ValueRange{it->second, nullPtr}); |
| 184 | + d.erase(); |
| 185 | + } |
| 186 | + } |
| 187 | +}; |
| 188 | + |
| 189 | +} // namespace |
| 190 | + |
| 191 | +namespace xilinx { |
| 192 | +namespace air { |
| 193 | + |
| 194 | +std::unique_ptr<mlir::Pass> createAIRSymmetricAllocToMgpuPass() { |
| 195 | + return std::make_unique<AIRSymmetricAllocToMgpuPass>(); |
| 196 | +} |
| 197 | + |
| 198 | +} // namespace air |
| 199 | +} // namespace xilinx |
0 commit comments