|
| 1 | +//===- AIRTranslateToLLVMPass.cpp -------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Copyright (C) 2026, Advanced Micro Devices, Inc. All rights reserved. |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +// |
| 6 | +//===-----------------------------------------------------------------------===// |
| 7 | +// |
| 8 | +// Lower air.translate to memref-descriptor construction over a peer-rebased |
| 9 | +// pointer. |
| 10 | +// |
| 11 | +// For each `air.translate %src, %from, %to, %bases`: |
| 12 | +// 1. Extract the source memref's aligned pointer as !llvm.ptr. |
| 13 | +// 2. Compute the byte diff between the per-rank base pointers from the |
| 14 | +// `$heap_bases` table: |
| 15 | +// byte_diff = ptrtoint(bases[to]) - ptrtoint(bases[from]) |
| 16 | +// 3. Apply the byte diff to the source aligned pointer (i8 GEP) to obtain |
| 17 | +// the peer aligned pointer. |
| 18 | +// 4. Build a fresh LLVM memref descriptor (poison + insertvalue chain) |
| 19 | +// whose allocated/aligned pointers both point at the peer address; the |
| 20 | +// offset is 0, and sizes/strides are taken from the source memref's |
| 21 | +// static type. |
| 22 | +// 5. unrealized_conversion_cast the descriptor back to the result memref |
| 23 | +// type so downstream uses keep working through the standard |
| 24 | +// memref-to-llvm pipeline. |
| 25 | +// |
| 26 | +// The lowering only uses arith + memref + llvm dialect ops — no runtime |
| 27 | +// calls. It is therefore valid both at host scope and inside `gpu.func` |
| 28 | +// (the kernel must already have been given the heap_bases pointer as a |
| 29 | +// kernel argument). |
| 30 | +// |
| 31 | +//===-----------------------------------------------------------------------===// |
| 32 | + |
| 33 | +#include "air/Conversion/AIRTranslateToLLVMPass.h" |
| 34 | +#include "air/Conversion/GPUPassDetail.h" |
| 35 | +#include "air/Dialect/AIR/AIRDialect.h" |
| 36 | + |
| 37 | +#include "mlir/Dialect/Arith/IR/Arith.h" |
| 38 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 39 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 40 | +#include "mlir/IR/Builders.h" |
| 41 | +#include "mlir/IR/BuiltinOps.h" |
| 42 | +#include "mlir/Pass/Pass.h" |
| 43 | + |
| 44 | +using namespace mlir; |
| 45 | +using namespace xilinx; |
| 46 | + |
| 47 | +namespace { |
| 48 | + |
| 49 | +// Build a fresh LLVM memref descriptor for `memrefTy` whose |
| 50 | +// allocated_ptr and aligned_ptr both reference `ptr`, offset is 0, and |
| 51 | +// sizes/strides come from the static type (row-major). |
| 52 | +// |
| 53 | +// Mirrors buildMemrefDescriptor in AIRSymmetricAllocToMgpuPass. |
| 54 | +static Value buildPeerDescriptor(OpBuilder &b, Location loc, |
| 55 | + MemRefType memrefTy, Value ptr) { |
| 56 | + ArrayRef<int64_t> shape = memrefTy.getShape(); |
| 57 | + unsigned rank = shape.size(); |
| 58 | + auto i64Ty = b.getI64Type(); |
| 59 | + auto ptrTy = LLVM::LLVMPointerType::get(b.getContext()); |
| 60 | + |
| 61 | + SmallVector<Type, 5> descFields; |
| 62 | + descFields.push_back(ptrTy); |
| 63 | + descFields.push_back(ptrTy); |
| 64 | + descFields.push_back(i64Ty); |
| 65 | + if (rank > 0) { |
| 66 | + descFields.push_back(LLVM::LLVMArrayType::get(i64Ty, rank)); |
| 67 | + descFields.push_back(LLVM::LLVMArrayType::get(i64Ty, rank)); |
| 68 | + } |
| 69 | + auto structTy = LLVM::LLVMStructType::getLiteral(b.getContext(), descFields); |
| 70 | + |
| 71 | + Value desc = LLVM::PoisonOp::create(b, loc, structTy); |
| 72 | + desc = LLVM::InsertValueOp::create(b, loc, desc, ptr, ArrayRef<int64_t>{0}); |
| 73 | + desc = LLVM::InsertValueOp::create(b, loc, desc, ptr, ArrayRef<int64_t>{1}); |
| 74 | + Value zero = LLVM::ConstantOp::create(b, loc, i64Ty, b.getI64IntegerAttr(0)); |
| 75 | + desc = LLVM::InsertValueOp::create(b, loc, desc, zero, ArrayRef<int64_t>{2}); |
| 76 | + |
| 77 | + if (rank > 0) { |
| 78 | + SmallVector<int64_t> strides(rank, 1); |
| 79 | + for (int i = static_cast<int>(rank) - 2; i >= 0; --i) |
| 80 | + strides[i] = strides[i + 1] * shape[i + 1]; |
| 81 | + for (unsigned i = 0; i < rank; ++i) { |
| 82 | + Value sz = LLVM::ConstantOp::create(b, loc, i64Ty, |
| 83 | + b.getI64IntegerAttr(shape[i])); |
| 84 | + desc = LLVM::InsertValueOp::create(b, loc, desc, sz, |
| 85 | + ArrayRef<int64_t>{3, (int64_t)i}); |
| 86 | + Value st = LLVM::ConstantOp::create(b, loc, i64Ty, |
| 87 | + b.getI64IntegerAttr(strides[i])); |
| 88 | + desc = LLVM::InsertValueOp::create(b, loc, desc, st, |
| 89 | + ArrayRef<int64_t>{4, (int64_t)i}); |
| 90 | + } |
| 91 | + } |
| 92 | + return desc; |
| 93 | +} |
| 94 | + |
| 95 | +struct AIRTranslateToLLVMPass |
| 96 | + : public xilinx::air::impl::AIRTranslateToLLVMBase<AIRTranslateToLLVMPass> { |
| 97 | + |
| 98 | + AIRTranslateToLLVMPass() = default; |
| 99 | + AIRTranslateToLLVMPass(const AIRTranslateToLLVMPass &) {} |
| 100 | + |
| 101 | + void runOnOperation() override { |
| 102 | + auto module = getOperation(); |
| 103 | + auto *ctx = module.getContext(); |
| 104 | + OpBuilder builder(ctx); |
| 105 | + auto i64Ty = builder.getI64Type(); |
| 106 | + auto ptrTy = LLVM::LLVMPointerType::get(ctx); |
| 107 | + |
| 108 | + SmallVector<air::TranslateOp> translates; |
| 109 | + module.walk([&](air::TranslateOp op) { translates.push_back(op); }); |
| 110 | + if (translates.empty()) |
| 111 | + return; |
| 112 | + |
| 113 | + for (air::TranslateOp op : translates) { |
| 114 | + builder.setInsertionPoint(op); |
| 115 | + Location loc = op.getLoc(); |
| 116 | + |
| 117 | + auto memrefTy = cast<MemRefType>(op.getSource().getType()); |
| 118 | + if (!memrefTy.hasStaticShape()) { |
| 119 | + op.emitOpError("air.translate requires a static-shape source memref"); |
| 120 | + signalPassFailure(); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // Extract source aligned pointer as !llvm.ptr. |
| 125 | + Value srcAlignedIdx = memref::ExtractAlignedPointerAsIndexOp::create( |
| 126 | + builder, loc, op.getSource()); |
| 127 | + Value srcAlignedI64 = arith::IndexCastOp::create(builder, loc, i64Ty, |
| 128 | + srcAlignedIdx); |
| 129 | + Value srcAlignedPtr = |
| 130 | + LLVM::IntToPtrOp::create(builder, loc, ptrTy, srcAlignedI64); |
| 131 | + |
| 132 | + // Load bases[from] and bases[to]. |
| 133 | + Value fromI64 = arith::IndexCastOp::create(builder, loc, i64Ty, |
| 134 | + op.getFromRank()); |
| 135 | + Value toI64 = arith::IndexCastOp::create(builder, loc, i64Ty, |
| 136 | + op.getToRank()); |
| 137 | + Value fromBaseAddr = LLVM::GEPOp::create( |
| 138 | + builder, loc, ptrTy, ptrTy, op.getHeapBases(), ValueRange{fromI64}); |
| 139 | + Value fromBase = LLVM::LoadOp::create(builder, loc, ptrTy, fromBaseAddr); |
| 140 | + Value toBaseAddr = LLVM::GEPOp::create(builder, loc, ptrTy, ptrTy, |
| 141 | + op.getHeapBases(), |
| 142 | + ValueRange{toI64}); |
| 143 | + Value toBase = LLVM::LoadOp::create(builder, loc, ptrTy, toBaseAddr); |
| 144 | + |
| 145 | + // byte_diff = ptrtoint(toBase) - ptrtoint(fromBase) |
| 146 | + Value fromInt = LLVM::PtrToIntOp::create(builder, loc, i64Ty, fromBase); |
| 147 | + Value toInt = LLVM::PtrToIntOp::create(builder, loc, i64Ty, toBase); |
| 148 | + Value byteDiff = arith::SubIOp::create(builder, loc, toInt, fromInt); |
| 149 | + |
| 150 | + // peer_aligned_ptr = srcAlignedPtr + byteDiff (as i8 GEP) |
| 151 | + auto i8Ty = builder.getI8Type(); |
| 152 | + Value peerAlignedPtr = LLVM::GEPOp::create( |
| 153 | + builder, loc, ptrTy, i8Ty, srcAlignedPtr, ValueRange{byteDiff}); |
| 154 | + |
| 155 | + // Build a fresh memref descriptor with the peer aligned pointer. |
| 156 | + Value desc = buildPeerDescriptor(builder, loc, memrefTy, peerAlignedPtr); |
| 157 | + Value newMemref = |
| 158 | + UnrealizedConversionCastOp::create(builder, loc, |
| 159 | + TypeRange{memrefTy}, |
| 160 | + ValueRange{desc}) |
| 161 | + .getResult(0); |
| 162 | + |
| 163 | + op.getResult().replaceAllUsesWith(newMemref); |
| 164 | + op.erase(); |
| 165 | + } |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +} // namespace |
| 170 | + |
| 171 | +namespace xilinx { |
| 172 | +namespace air { |
| 173 | + |
| 174 | +std::unique_ptr<mlir::Pass> createAIRTranslateToLLVMPass() { |
| 175 | + return std::make_unique<AIRTranslateToLLVMPass>(); |
| 176 | +} |
| 177 | + |
| 178 | +} // namespace air |
| 179 | +} // namespace xilinx |
0 commit comments