Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.

Commit 75bf9ca

Browse files
smit-hinsutensorflower-gardener
authored andcommitted
Generalize I32ElementsAttr definition and introduce I64ElementsAttr
Also, fix constBuilderCall to return attribute of the storage class DenseIntElementsAttr PiperOrigin-RevId: 267305813
1 parent 72e261b commit 75bf9ca

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

include/mlir/IR/OpBase.td

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -866,9 +866,28 @@ class ElementsAttrBase<Pred condition, string description> :
866866
let convertFromStorage = "$_self";
867867
}
868868

869-
def ElementsAttr: ElementsAttrBase<CPred<"$_self.isa<ElementsAttr>()">,
869+
def ElementsAttr : ElementsAttrBase<CPred<"$_self.isa<ElementsAttr>()">,
870870
"constant vector/tensor attribute">;
871871

872+
class IntElementsAttr<int width> : ElementsAttrBase<
873+
CPred<"$_self.isa<DenseIntElementsAttr>() &&"
874+
"$_self.cast<DenseIntElementsAttr>().getType()."
875+
"getElementType().isInteger(" # width # ")">,
876+
width # "-bit integer elements attribute"> {
877+
878+
let storageType = [{ DenseIntElementsAttr }];
879+
let returnType = [{ DenseIntElementsAttr }];
880+
881+
// Note that this is only constructing scalar elements attribute.
882+
let constBuilderCall = "DenseElementsAttr::get("
883+
"$_builder.getTensorType({}, $_builder.getIntegerType(" # width # ")), "
884+
"llvm::makeArrayRef($0)).cast<DenseIntElementsAttr>()";
885+
let convertFromStorage = "$_self";
886+
}
887+
888+
def I32ElementsAttr : IntElementsAttr<32>;
889+
def I64ElementsAttr : IntElementsAttr<64>;
890+
872891
// Base class for array attributes.
873892
class ArrayAttrBase<Pred condition, string description> :
874893
Attr<condition, description> {
@@ -916,18 +935,6 @@ def TypeArrayAttr : TypedArrayAttrBase<TypeAttr, "type array attribute"> {
916935
let constBuilderCall = ?;
917936
}
918937

919-
def I32ElementsAttr : Attr<
920-
CPred<"$_self.isa<DenseIntElementsAttr>() &&"
921-
"$_self.cast<DenseIntElementsAttr>().getType()."
922-
"getElementType().isInteger(32)">,
923-
"32-bit integer elements attribute"> {
924-
let storageType = [{ DenseIntElementsAttr }];
925-
let returnType = [{ DenseIntElementsAttr }];
926-
let constBuilderCall = "$_builder.getDenseElementsAttr("
927-
"$_builder.getTensorType({}, $_builder.getIntegerType(32)), "
928-
"{$_builder.getI32IntegerAttr($0)})";
929-
let convertFromStorage = "$_self";
930-
}
931938
// Attribute information for an Attribute field within a StructAttr.
932939
class StructFieldAttr<string thisName, Attr thisType> {
933940
// Name of this field in the StructAttr.

test/lib/TestDialect/TestOps.td

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,16 @@ def SingleBlockImplicitTerminatorOp : TEST_Op<"SingleBlockImplicitTerminator",
263263
let regions = (region SizedRegion<1>:$region);
264264
}
265265

266-
def I32ElementsAttributesOp : TEST_Op<"i32ElementsAttr"> {
266+
def I32ElementsAttrOp : TEST_Op<"i32ElementsAttr"> {
267267
let arguments = (ins I32ElementsAttr:$attr);
268268
}
269269

270+
def IsNotScalar : Constraint<CPred<"$0.getType().getRank() != 0">>;
271+
272+
def UpdateAttr : Pat<(I32ElementsAttrOp $attr),
273+
(I32ElementsAttrOp ConstantAttr<I32ElementsAttr, "0">),
274+
[(IsNotScalar $attr)]>;
275+
270276
//===----------------------------------------------------------------------===//
271277
// Test Patterns
272278
//===----------------------------------------------------------------------===//

test/mlir-tblgen/pattern.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ func @verifyI64EnumAttr() -> i32 {
152152
return %0 : i32
153153
}
154154

155+
//===----------------------------------------------------------------------===//
156+
// Test ElelementsAttr
157+
//===----------------------------------------------------------------------===//
158+
159+
// CHECK-LABEL: rewrite_i32elementsattr
160+
func @rewrite_i32elementsattr() -> () {
161+
// CHECK: attr = dense<0> : tensor<i32>
162+
"test.i32ElementsAttr"() {attr = dense<[3, 5]>:tensor<2xi32>} : () -> ()
163+
return
164+
}
165+
155166
//===----------------------------------------------------------------------===//
156167
// Test Multi-result Ops
157168
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)