28
28
#include <stdlib.h>
29
29
#include <string.h>
30
30
31
- MlirValue makeConstantLiteral (MlirContext ctx , const char * literalStr ,
32
- const char * typeStr ) {
33
- MlirLocation loc = mlirLocationUnknownGet (ctx );
34
- char attrStr [50 ];
35
- sprintf (attrStr , "%s : %s" , literalStr , typeStr );
36
- MlirAttribute literal =
37
- mlirAttributeParseGet (ctx , mlirStringRefCreateFromCString (attrStr ));
38
- MlirNamedAttribute valueAttr = mlirNamedAttributeGet (
39
- mlirIdentifierGet (ctx , mlirStringRefCreateFromCString ("value" )), literal );
40
- MlirOperationState constState = mlirOperationStateGet (
41
- mlirStringRefCreateFromCString ("arith.constant" ), loc );
42
- MlirType type =
43
- mlirTypeParseGet (ctx , mlirStringRefCreateFromCString (typeStr ));
44
- mlirOperationStateAddResults (& constState , 1 , & type );
45
- mlirOperationStateAddAttributes (& constState , 1 , & valueAttr );
46
- MlirOperation constOp = mlirOperationCreate (& constState );
47
- return mlirOperationGetResult (constOp , 0 );
48
- }
49
-
50
31
static void registerAllUpstreamDialects (MlirContext ctx ) {
51
32
MlirDialectRegistry registry = mlirDialectRegistryCreate ();
52
33
mlirRegisterAllDialects (registry );
@@ -134,17 +115,26 @@ MlirModule makeAndDumpAdd(MlirContext ctx, MlirLocation location) {
134
115
MlirOperation func = mlirOperationCreate (& funcState );
135
116
mlirBlockInsertOwnedOperation (moduleBody , 0 , func );
136
117
137
- MlirValue constZeroValue = makeConstantLiteral (ctx , "0" , "index" );
138
- MlirOperation constZero = mlirOpResultGetOwner (constZeroValue );
118
+ MlirType indexType =
119
+ mlirTypeParseGet (ctx , mlirStringRefCreateFromCString ("index" ));
120
+ MlirAttribute indexZeroLiteral =
121
+ mlirAttributeParseGet (ctx , mlirStringRefCreateFromCString ("0 : index" ));
122
+ MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet (
123
+ mlirIdentifierGet (ctx , mlirStringRefCreateFromCString ("value" )),
124
+ indexZeroLiteral );
125
+ MlirOperationState constZeroState = mlirOperationStateGet (
126
+ mlirStringRefCreateFromCString ("arith.constant" ), location );
127
+ mlirOperationStateAddResults (& constZeroState , 1 , & indexType );
128
+ mlirOperationStateAddAttributes (& constZeroState , 1 , & indexZeroValueAttr );
129
+ MlirOperation constZero = mlirOperationCreate (& constZeroState );
139
130
mlirBlockAppendOwnedOperation (funcBody , constZero );
140
131
141
132
MlirValue funcArg0 = mlirBlockGetArgument (funcBody , 0 );
133
+ MlirValue constZeroValue = mlirOperationGetResult (constZero , 0 );
142
134
MlirValue dimOperands [] = {funcArg0 , constZeroValue };
143
135
MlirOperationState dimState = mlirOperationStateGet (
144
136
mlirStringRefCreateFromCString ("memref.dim" ), location );
145
137
mlirOperationStateAddOperands (& dimState , 2 , dimOperands );
146
- MlirType indexType =
147
- mlirTypeParseGet (ctx , mlirStringRefCreateFromCString ("index" ));
148
138
mlirOperationStateAddResults (& dimState , 1 , & indexType );
149
139
MlirOperation dim = mlirOperationCreate (& dimState );
150
140
mlirBlockAppendOwnedOperation (funcBody , dim );
@@ -163,11 +153,11 @@ MlirModule makeAndDumpAdd(MlirContext ctx, MlirLocation location) {
163
153
mlirStringRefCreateFromCString ("arith.constant" ), location );
164
154
mlirOperationStateAddResults (& constOneState , 1 , & indexType );
165
155
mlirOperationStateAddAttributes (& constOneState , 1 , & indexOneValueAttr );
166
- MlirValue constOneValue = makeConstantLiteral (ctx , "1" , "index" );
167
- MlirOperation constOne = mlirOpResultGetOwner (constOneValue );
156
+ MlirOperation constOne = mlirOperationCreate (& constOneState );
168
157
mlirBlockAppendOwnedOperation (funcBody , constOne );
169
158
170
159
MlirValue dimValue = mlirOperationGetResult (dim , 0 );
160
+ MlirValue constOneValue = mlirOperationGetResult (constOne , 0 );
171
161
MlirValue loopOperands [] = {constZeroValue , dimValue , constOneValue };
172
162
MlirOperationState loopState = mlirOperationStateGet (
173
163
mlirStringRefCreateFromCString ("scf.for" ), location );
@@ -830,6 +820,11 @@ static int printBuiltinTypes(MlirContext ctx) {
830
820
return 0 ;
831
821
}
832
822
823
+ void callbackSetFixedLengthString (const char * data , intptr_t len ,
824
+ void * userData ) {
825
+ strncpy (userData , data , len );
826
+ }
827
+
833
828
bool stringIsEqual (const char * lhs , MlirStringRef rhs ) {
834
829
if (strlen (lhs ) != rhs .length ) {
835
830
return false;
@@ -1799,10 +1794,32 @@ int testOperands(void) {
1799
1794
mlirContextGetOrLoadDialect (ctx , mlirStringRefCreateFromCString ("arith" ));
1800
1795
mlirContextGetOrLoadDialect (ctx , mlirStringRefCreateFromCString ("test" ));
1801
1796
MlirLocation loc = mlirLocationUnknownGet (ctx );
1797
+ MlirType indexType = mlirIndexTypeGet (ctx );
1802
1798
1803
1799
// Create some constants to use as operands.
1804
- MlirValue constZeroValue = makeConstantLiteral (ctx , "0" , "index" );
1805
- MlirValue constOneValue = makeConstantLiteral (ctx , "1" , "index" );
1800
+ MlirAttribute indexZeroLiteral =
1801
+ mlirAttributeParseGet (ctx , mlirStringRefCreateFromCString ("0 : index" ));
1802
+ MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet (
1803
+ mlirIdentifierGet (ctx , mlirStringRefCreateFromCString ("value" )),
1804
+ indexZeroLiteral );
1805
+ MlirOperationState constZeroState = mlirOperationStateGet (
1806
+ mlirStringRefCreateFromCString ("arith.constant" ), loc );
1807
+ mlirOperationStateAddResults (& constZeroState , 1 , & indexType );
1808
+ mlirOperationStateAddAttributes (& constZeroState , 1 , & indexZeroValueAttr );
1809
+ MlirOperation constZero = mlirOperationCreate (& constZeroState );
1810
+ MlirValue constZeroValue = mlirOperationGetResult (constZero , 0 );
1811
+
1812
+ MlirAttribute indexOneLiteral =
1813
+ mlirAttributeParseGet (ctx , mlirStringRefCreateFromCString ("1 : index" ));
1814
+ MlirNamedAttribute indexOneValueAttr = mlirNamedAttributeGet (
1815
+ mlirIdentifierGet (ctx , mlirStringRefCreateFromCString ("value" )),
1816
+ indexOneLiteral );
1817
+ MlirOperationState constOneState = mlirOperationStateGet (
1818
+ mlirStringRefCreateFromCString ("arith.constant" ), loc );
1819
+ mlirOperationStateAddResults (& constOneState , 1 , & indexType );
1820
+ mlirOperationStateAddAttributes (& constOneState , 1 , & indexOneValueAttr );
1821
+ MlirOperation constOne = mlirOperationCreate (& constOneState );
1822
+ MlirValue constOneValue = mlirOperationGetResult (constOne , 0 );
1806
1823
1807
1824
// Create the operation under test.
1808
1825
mlirContextSetAllowUnregisteredDialects (ctx , true);
@@ -1856,50 +1873,9 @@ int testOperands(void) {
1856
1873
return 3 ;
1857
1874
}
1858
1875
1859
- MlirOperationState op2State =
1860
- mlirOperationStateGet (mlirStringRefCreateFromCString ("dummy.op2" ), loc );
1861
- MlirValue initialOperands2 [] = {constOneValue };
1862
- mlirOperationStateAddOperands (& op2State , 1 , initialOperands2 );
1863
- MlirOperation op2 = mlirOperationCreate (& op2State );
1864
-
1865
- MlirOpOperand use3 = mlirValueGetFirstUse (constOneValue );
1866
- fprintf (stderr , "First use owner: " );
1867
- mlirOperationPrint (mlirOpOperandGetOwner (use3 ), printToStderr , NULL );
1868
- fprintf (stderr , "\n" );
1869
- // CHECK: First use owner: "dummy.op2"
1870
-
1871
- use3 = mlirOpOperandGetNextUse (mlirValueGetFirstUse (constOneValue ));
1872
- fprintf (stderr , "Second use owner: " );
1873
- mlirOperationPrint (mlirOpOperandGetOwner (use3 ), printToStderr , NULL );
1874
- fprintf (stderr , "\n" );
1875
- // CHECK: Second use owner: "dummy.op"
1876
-
1877
- MlirValue constTwoValue = makeConstantLiteral (ctx , "2" , "index" );
1878
- mlirValueReplaceAllUsesOfWith (constOneValue , constTwoValue );
1879
-
1880
- use3 = mlirValueGetFirstUse (constOneValue );
1881
- if (!mlirOpOperandIsNull (use3 )) {
1882
- fprintf (stderr , "ERROR: Use should be null\n" );
1883
- return 4 ;
1884
- }
1885
-
1886
- MlirOpOperand use4 = mlirValueGetFirstUse (constTwoValue );
1887
- fprintf (stderr , "First replacement use owner: " );
1888
- mlirOperationPrint (mlirOpOperandGetOwner (use4 ), printToStderr , NULL );
1889
- fprintf (stderr , "\n" );
1890
- // CHECK: First replacement use owner: "dummy.op"
1891
-
1892
- use4 = mlirOpOperandGetNextUse (mlirValueGetFirstUse (constTwoValue ));
1893
- fprintf (stderr , "Second replacement use owner: " );
1894
- mlirOperationPrint (mlirOpOperandGetOwner (use4 ), printToStderr , NULL );
1895
- fprintf (stderr , "\n" );
1896
- // CHECK: Second replacement use owner: "dummy.op2"
1897
-
1898
1876
mlirOperationDestroy (op );
1899
- mlirOperationDestroy (op2 );
1900
- mlirOperationDestroy (mlirOpResultGetOwner (constZeroValue ));
1901
- mlirOperationDestroy (mlirOpResultGetOwner (constOneValue ));
1902
- mlirOperationDestroy (mlirOpResultGetOwner (constTwoValue ));
1877
+ mlirOperationDestroy (constZero );
1878
+ mlirOperationDestroy (constOne );
1903
1879
mlirContextDestroy (ctx );
1904
1880
1905
1881
return 0 ;
@@ -1914,10 +1890,19 @@ int testClone(void) {
1914
1890
registerAllUpstreamDialects (ctx );
1915
1891
1916
1892
mlirContextGetOrLoadDialect (ctx , mlirStringRefCreateFromCString ("func" ));
1893
+ MlirLocation loc = mlirLocationUnknownGet (ctx );
1894
+ MlirType indexType = mlirIndexTypeGet (ctx );
1917
1895
MlirStringRef valueStringRef = mlirStringRefCreateFromCString ("value" );
1918
1896
1919
- MlirValue constZeroValue = makeConstantLiteral (ctx , "0" , "index" );
1920
- MlirOperation constZero = mlirOpResultGetOwner (constZeroValue );
1897
+ MlirAttribute indexZeroLiteral =
1898
+ mlirAttributeParseGet (ctx , mlirStringRefCreateFromCString ("0 : index" ));
1899
+ MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet (
1900
+ mlirIdentifierGet (ctx , valueStringRef ), indexZeroLiteral );
1901
+ MlirOperationState constZeroState = mlirOperationStateGet (
1902
+ mlirStringRefCreateFromCString ("arith.constant" ), loc );
1903
+ mlirOperationStateAddResults (& constZeroState , 1 , & indexType );
1904
+ mlirOperationStateAddAttributes (& constZeroState , 1 , & indexZeroValueAttr );
1905
+ MlirOperation constZero = mlirOperationCreate (& constZeroState );
1921
1906
1922
1907
MlirAttribute indexOneLiteral =
1923
1908
mlirAttributeParseGet (ctx , mlirStringRefCreateFromCString ("1 : index" ));
@@ -1995,10 +1980,19 @@ int testTypeID(MlirContext ctx) {
1995
1980
}
1996
1981
1997
1982
MlirLocation loc = mlirLocationUnknownGet (ctx );
1983
+ MlirType indexType = mlirIndexTypeGet (ctx );
1984
+ MlirStringRef valueStringRef = mlirStringRefCreateFromCString ("value" );
1998
1985
1999
1986
// Create a registered operation, which should have a type id.
2000
- MlirValue constZeroValue = makeConstantLiteral (ctx , "0" , "index" );
2001
- MlirOperation constZero = mlirOpResultGetOwner (constZeroValue );
1987
+ MlirAttribute indexZeroLiteral =
1988
+ mlirAttributeParseGet (ctx , mlirStringRefCreateFromCString ("0 : index" ));
1989
+ MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet (
1990
+ mlirIdentifierGet (ctx , valueStringRef ), indexZeroLiteral );
1991
+ MlirOperationState constZeroState = mlirOperationStateGet (
1992
+ mlirStringRefCreateFromCString ("arith.constant" ), loc );
1993
+ mlirOperationStateAddResults (& constZeroState , 1 , & indexType );
1994
+ mlirOperationStateAddAttributes (& constZeroState , 1 , & indexZeroValueAttr );
1995
+ MlirOperation constZero = mlirOperationCreate (& constZeroState );
2002
1996
2003
1997
if (!mlirOperationVerify (constZero )) {
2004
1998
fprintf (stderr , "ERROR: Expected operation to verify correctly\n" );
0 commit comments