[Mlir-commits] [mlir] [mlir][SPIRV] Lower ptr dialect to PBA addresses (PR #206159)

Igor Wodiany llvmlistbot at llvm.org
Mon Jun 29 02:05:35 PDT 2026


================
@@ -0,0 +1,297 @@
+//===- PtrToSPIRV.cpp - Ptr to SPIR-V dialect conversion -----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Conversion/PtrToSPIRV/PtrToSPIRV.h"
+
+#include "mlir/Dialect/Ptr/IR/PtrOps.h"
+#include "mlir/Dialect/Ptr/IR/PtrTypes.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
+#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
+#include "mlir/Transforms/DialectConversion.h"
+#include "llvm/ADT/StringRef.h"
+#include <limits>
+
+namespace mlir {
+#define GEN_PASS_DEF_CONVERTPTRTOSPIRVPASS
+#include "mlir/Conversion/Passes.h.inc"
+} // namespace mlir
+
+using namespace mlir;
+
+namespace {
+
+static FailureOr<Type> getAddressType(spirv::TargetEnvAttr targetAttr,
+                                      MLIRContext *context) {
+  spirv::AddressingModel addressingModel =
+      spirv::getAddressingModel(targetAttr, /*use64bitAddress=*/true);
+  if (addressingModel == spirv::AddressingModel::PhysicalStorageBuffer64)
+    return IntegerType::get(context, 64);
+
+  return failure();
+}
+
+static LogicalResult getMemoryAccessAttrs(std::optional<int64_t> alignment,
+                                          Builder &builder,
+                                          spirv::MemoryAccessAttr &accessAttr,
+                                          IntegerAttr &alignmentAttr) {
+  if (!alignment)
+    return success();
+  if (*alignment > std::numeric_limits<uint32_t>::max())
+    return failure();
+
+  accessAttr = spirv::MemoryAccessAttr::get(builder.getContext(),
+                                            spirv::MemoryAccess::Aligned);
+  alignmentAttr = builder.getI32IntegerAttr(*alignment);
+  return success();
+}
+
+static LogicalResult checkSupportedPtrLoad(ptr::LoadOp op,
+                                           PatternRewriter &rewriter) {
+  if (op.getVolatile_() || op.getNontemporal() || op.getInvariant() ||
+      op.getInvariantGroup())
+    return rewriter.notifyMatchFailure(
+        op, "unsupported ptr.load memory operand for SPIR-V lowering");
+  if (op.getOrdering() != ptr::AtomicOrdering::not_atomic)
+    return rewriter.notifyMatchFailure(
+        op, "unsupported atomic ptr.load for SPIR-V lowering");
+  return success();
+}
+
+static LogicalResult checkSupportedPtrStore(ptr::StoreOp op,
+                                            PatternRewriter &rewriter) {
+  if (op.getVolatile_() || op.getNontemporal() || op.getInvariantGroup())
+    return rewriter.notifyMatchFailure(
+        op, "unsupported ptr.store memory operand for SPIR-V lowering");
+  if (op.getOrdering() != ptr::AtomicOrdering::not_atomic)
+    return rewriter.notifyMatchFailure(
+        op, "unsupported atomic ptr.store for SPIR-V lowering");
+  return success();
+}
+
+static FailureOr<Value>
+castAddressToPointeeType(Operation *op, Value address, Type pointeeType,
+                         spirv::StorageClass storageClass, Location loc,
+                         PatternRewriter &rewriter) {
+  if (!isa<IntegerType>(address.getType())) {
+    (void)rewriter.notifyMatchFailure(op, "expected integer address operand");
+    return failure();
----------------
IgWod wrote:

```suggestion
    return rewriter.notifyMatchFailure(op, "expected integer address operand");
```

Does that work?

https://github.com/llvm/llvm-project/pull/206159


More information about the Mlir-commits mailing list