Skip to content

[memref] Support dense resources for memref-to-llvm #79380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "mlir/Dialect/MemRef/Utils/MemRefUtils.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectResourceBlobManager.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Support/MathExtras.h"
Expand Down Expand Up @@ -501,13 +502,22 @@ struct GlobalMemrefOpLowering

Attribute initialValue = nullptr;
if (!global.isExternal() && !global.isUninitialized()) {
auto elementsAttr = llvm::cast<ElementsAttr>(*global.getInitialValue());
initialValue = elementsAttr;
Attribute initialAttr = *global.getInitialValue();
if (auto resourceAttr =
llvm::dyn_cast<DenseResourceElementsAttr>(initialAttr)) {
auto blob = resourceAttr.getRawHandle().getBlob();
initialAttr = DenseElementsAttr::getFromRawBuffer(
resourceAttr.getType(), blob->getData());
}

if (auto elementsAttr = llvm::cast<ElementsAttr>(initialAttr)) {
initialValue = elementsAttr;

// For scalar memrefs, the global variable created is of the element type,
// so unpack the elements attribute to extract the value.
if (type.getRank() == 0)
initialValue = elementsAttr.getSplatValue<Attribute>();
// For scalar memrefs, the global variable created is of the element
// type, so unpack the elements attribute to extract the value.
if (type.getRank() == 0)
initialValue = elementsAttr.getSplatValue<Attribute>();
}
}

uint64_t alignment = global.getAlignment().value_or(0);
Expand Down
18 changes: 18 additions & 0 deletions mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,24 @@ memref.global "private" @gv4 : memref<f32> = dense<1.0> {alignment = 64}

// -----

module {
// CHECK: llvm.mlir.global private constant @__constant_xf32_0(1.41421354 : f32) {addr_space = 0 : i32} : f32
memref.global "private" constant @__constant_xf32_0 : memref<f32> = dense_resource<NAME0>
// CHECK: llvm.mlir.global private constant @__constant_xf32_1(dense<[5.000000e-01, 2.500000e-01]> : tensor<2xf32>) {addr_space = 0 : i32} : !llvm.array<2 x f32>
memref.global "private" constant @__constant_xf32_1 : memref<2xf32> = dense_resource<NAME1>
}

{-#
dialect_resources: {
builtin: {
NAME0: "0x08000000F304B53F",
NAME1: "0x080000000000003F0000803E"
}
}
#-}

// -----

// Expand shapes need to be expanded outside of the memref-to-llvm pass.
// CHECK-LABEL: func @expand_shape_static(
// CHECK-SAME: %[[ARG:.*]]: memref<{{.*}}>)
Expand Down