Skip to content

Conversation

@jacquesguan
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2025

@llvm/pr-subscribers-mlir-emitc

Author: Jianjian Guan (jacquesguan)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/144826.diff

2 Files Affected:

  • (modified) mlir/lib/Target/Cpp/TranslateToCpp.cpp (+14-4)
  • (modified) mlir/test/Target/Cpp/const.mlir (+8)
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 067a0470b14e4..480962afb5319 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -1328,6 +1328,16 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
     }
   };
 
+  auto getDenseElementType = [&](const Type &type) {
+    if (auto arrayType = dyn_cast<ArrayType>(type)) {
+      return arrayType.getElementType();
+    }
+    if (auto tensorType = dyn_cast<TensorType>(type)) {
+      return tensorType.getElementType();
+    }
+    return Type();
+  };
+
   // Print floating point attributes.
   if (auto fAttr = dyn_cast<FloatAttr>(attr)) {
     if (!isa<Float16Type, BFloat16Type, Float32Type, Float64Type>(
@@ -1362,8 +1372,8 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
     }
   }
   if (auto dense = dyn_cast<DenseIntElementsAttr>(attr)) {
-    if (auto iType = dyn_cast<IntegerType>(
-            cast<TensorType>(dense.getType()).getElementType())) {
+    if (auto iType =
+            dyn_cast<IntegerType>(getDenseElementType(dense.getType()))) {
       os << '{';
       interleaveComma(dense, os, [&](const APInt &val) {
         printInt(val, shouldMapToUnsigned(iType.getSignedness()));
@@ -1371,8 +1381,8 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
       os << '}';
       return success();
     }
-    if (auto iType = dyn_cast<IndexType>(
-            cast<TensorType>(dense.getType()).getElementType())) {
+    if (auto iType =
+            dyn_cast<IndexType>(getDenseElementType(dense.getType()))) {
       os << '{';
       interleaveComma(dense, os,
                       [&](const APInt &val) { printInt(val, false); });
diff --git a/mlir/test/Target/Cpp/const.mlir b/mlir/test/Target/Cpp/const.mlir
index d3656f830c48c..2a5ff1a0f0177 100644
--- a/mlir/test/Target/Cpp/const.mlir
+++ b/mlir/test/Target/Cpp/const.mlir
@@ -16,6 +16,8 @@ func.func @emitc_constant() {
   %c8 = "emitc.constant"(){value = dense<0> : tensor<i32>} : () -> tensor<i32>
   %c9 = "emitc.constant"(){value = dense<[0, 1]> : tensor<2xindex>} : () -> tensor<2xindex>
   %c10 = "emitc.constant"(){value = dense<[[0.0, 1.0], [2.0, 3.0]]> : tensor<2x2xf32>} : () -> tensor<2x2xf32>
+  %c11 = "emitc.constant"(){value = dense<[0, 1]> : !emitc.array<2xindex>} : () -> !emitc.array<2xindex>
+  %c12 = "emitc.constant"(){value = dense<[0.0, 1.0]> : !emitc.array<2xf32>} : () -> !emitc.array<2xf32>
   return
 }
 // CPP-DEFAULT: void emitc_constant() {
@@ -33,6 +35,8 @@ func.func @emitc_constant() {
 // CPP-DEFAULT-NEXT: Tensor<int32_t> [[V8:[^ ]*]] = {0};
 // CPP-DEFAULT-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]] = {0, 1};
 // CPP-DEFAULT-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};
+// CPP-DEFAULT-NEXT: size_t [[V11:[^ ]*]][2] = {0, 1};
+// CPP-DEFAULT-NEXT: float [[V12:[^ ]*]][2] = {0.0e+00f, 1.000000000e+00f};
 
 // CPP-DECLTOP: void emitc_constant() {
 // CPP-DECLTOP-NEXT: int32_t [[V0:[^ ]*]];
@@ -49,6 +53,8 @@ func.func @emitc_constant() {
 // CPP-DECLTOP-NEXT: Tensor<int32_t> [[V8:[^ ]*]];
 // CPP-DECLTOP-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]];
 // CPP-DECLTOP-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]];
+// CPP-DECLTOP-NEXT: size_t [[V11:[^ ]*]][2];
+// CPP-DECLTOP-NEXT: float [[V12:[^ ]*]][2];
 // CPP-DECLTOP-NEXT: [[V0]] = INT_MAX;
 // CPP-DECLTOP-NEXT: [[V1]] = 42;
 // CPP-DECLTOP-NEXT: [[V2]] = -1;
@@ -63,3 +69,5 @@ func.func @emitc_constant() {
 // CPP-DECLTOP-NEXT: [[V8]] = {0};
 // CPP-DECLTOP-NEXT: [[V9]] = {0, 1};
 // CPP-DECLTOP-NEXT: [[V10]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};
+// CPP-DECLTOP-NEXT: [[V11]] = {0, 1};
+// CPP-DECLTOP-NEXT: [[V12]] = {0.0e+00f, 1.000000000e+00f};

@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2025

@llvm/pr-subscribers-mlir

Author: Jianjian Guan (jacquesguan)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/144826.diff

2 Files Affected:

  • (modified) mlir/lib/Target/Cpp/TranslateToCpp.cpp (+14-4)
  • (modified) mlir/test/Target/Cpp/const.mlir (+8)
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 067a0470b14e4..480962afb5319 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -1328,6 +1328,16 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
     }
   };
 
+  auto getDenseElementType = [&](const Type &type) {
+    if (auto arrayType = dyn_cast<ArrayType>(type)) {
+      return arrayType.getElementType();
+    }
+    if (auto tensorType = dyn_cast<TensorType>(type)) {
+      return tensorType.getElementType();
+    }
+    return Type();
+  };
+
   // Print floating point attributes.
   if (auto fAttr = dyn_cast<FloatAttr>(attr)) {
     if (!isa<Float16Type, BFloat16Type, Float32Type, Float64Type>(
@@ -1362,8 +1372,8 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
     }
   }
   if (auto dense = dyn_cast<DenseIntElementsAttr>(attr)) {
-    if (auto iType = dyn_cast<IntegerType>(
-            cast<TensorType>(dense.getType()).getElementType())) {
+    if (auto iType =
+            dyn_cast<IntegerType>(getDenseElementType(dense.getType()))) {
       os << '{';
       interleaveComma(dense, os, [&](const APInt &val) {
         printInt(val, shouldMapToUnsigned(iType.getSignedness()));
@@ -1371,8 +1381,8 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
       os << '}';
       return success();
     }
-    if (auto iType = dyn_cast<IndexType>(
-            cast<TensorType>(dense.getType()).getElementType())) {
+    if (auto iType =
+            dyn_cast<IndexType>(getDenseElementType(dense.getType()))) {
       os << '{';
       interleaveComma(dense, os,
                       [&](const APInt &val) { printInt(val, false); });
diff --git a/mlir/test/Target/Cpp/const.mlir b/mlir/test/Target/Cpp/const.mlir
index d3656f830c48c..2a5ff1a0f0177 100644
--- a/mlir/test/Target/Cpp/const.mlir
+++ b/mlir/test/Target/Cpp/const.mlir
@@ -16,6 +16,8 @@ func.func @emitc_constant() {
   %c8 = "emitc.constant"(){value = dense<0> : tensor<i32>} : () -> tensor<i32>
   %c9 = "emitc.constant"(){value = dense<[0, 1]> : tensor<2xindex>} : () -> tensor<2xindex>
   %c10 = "emitc.constant"(){value = dense<[[0.0, 1.0], [2.0, 3.0]]> : tensor<2x2xf32>} : () -> tensor<2x2xf32>
+  %c11 = "emitc.constant"(){value = dense<[0, 1]> : !emitc.array<2xindex>} : () -> !emitc.array<2xindex>
+  %c12 = "emitc.constant"(){value = dense<[0.0, 1.0]> : !emitc.array<2xf32>} : () -> !emitc.array<2xf32>
   return
 }
 // CPP-DEFAULT: void emitc_constant() {
@@ -33,6 +35,8 @@ func.func @emitc_constant() {
 // CPP-DEFAULT-NEXT: Tensor<int32_t> [[V8:[^ ]*]] = {0};
 // CPP-DEFAULT-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]] = {0, 1};
 // CPP-DEFAULT-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};
+// CPP-DEFAULT-NEXT: size_t [[V11:[^ ]*]][2] = {0, 1};
+// CPP-DEFAULT-NEXT: float [[V12:[^ ]*]][2] = {0.0e+00f, 1.000000000e+00f};
 
 // CPP-DECLTOP: void emitc_constant() {
 // CPP-DECLTOP-NEXT: int32_t [[V0:[^ ]*]];
@@ -49,6 +53,8 @@ func.func @emitc_constant() {
 // CPP-DECLTOP-NEXT: Tensor<int32_t> [[V8:[^ ]*]];
 // CPP-DECLTOP-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]];
 // CPP-DECLTOP-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]];
+// CPP-DECLTOP-NEXT: size_t [[V11:[^ ]*]][2];
+// CPP-DECLTOP-NEXT: float [[V12:[^ ]*]][2];
 // CPP-DECLTOP-NEXT: [[V0]] = INT_MAX;
 // CPP-DECLTOP-NEXT: [[V1]] = 42;
 // CPP-DECLTOP-NEXT: [[V2]] = -1;
@@ -63,3 +69,5 @@ func.func @emitc_constant() {
 // CPP-DECLTOP-NEXT: [[V8]] = {0};
 // CPP-DECLTOP-NEXT: [[V9]] = {0, 1};
 // CPP-DECLTOP-NEXT: [[V10]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};
+// CPP-DECLTOP-NEXT: [[V11]] = {0, 1};
+// CPP-DECLTOP-NEXT: [[V12]] = {0.0e+00f, 1.000000000e+00f};

@jacquesguan jacquesguan requested a review from marbre June 20, 2025 06:27
@tpopp tpopp removed their request for review June 20, 2025 09:42
Copy link
Contributor

@tpopp tpopp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one comment but removed myself as a reviewer as I'm not appropriate to guide or approve this.

Comment on lines 1331 to 1340
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be ShapedType instead? Also, it might be worth considering adding an assert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for comment, changed to use ShapedType and added assert.

@jacquesguan jacquesguan force-pushed the mlir-emitc-constant-dense branch from 2c07aa7 to 802cd0d Compare June 25, 2025 03:33
@jacquesguan
Copy link
Contributor Author

ping

@rengolin rengolin requested a review from aniragil August 12, 2025 08:32
Copy link
Contributor

@aniragil aniragil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, slipped under the radar.
Title mismatch? IIUC this patch is extending existing support of dense as init value from tensor type to shaped type?
LGTM with that assert nit. Curious about motivation: lowering? a step towards emitc-only module before translation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast asserts by itself, no need to assert separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@jacquesguan jacquesguan force-pushed the mlir-emitc-constant-dense branch from 802cd0d to ac19730 Compare August 18, 2025 09:03
@jacquesguan jacquesguan changed the title [mlir][emitc] Support dense as init value of constantOp [mlir][emitc] Support dense as init value for ShapedType Aug 18, 2025
@jacquesguan
Copy link
Contributor Author

Sorry for the delay, slipped under the radar. Title mismatch? IIUC this patch is extending existing support of dense as init value from tensor type to shaped type? LGTM with that assert nit. Curious about motivation: lowering? a step towards emitc-only module before translation?

Thanks for comment, title has been changed now. The motivation of this pr is lowering, for some custom ops, we need to generate some array type in emitc.

Copy link
Contributor

@aniragil aniragil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@jacquesguan jacquesguan merged commit 1eb5b18 into llvm:main Aug 19, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants