#include <stdlib.h>
#include <string.h>
-MlirValue makeConstantLiteral(MlirContext ctx, const char *literalStr,
- const char *typeStr) {
- MlirLocation loc = mlirLocationUnknownGet(ctx);
- char attrStr[50];
- sprintf(attrStr, "%s : %s", literalStr, typeStr);
- MlirAttribute literal =
- mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString(attrStr));
- MlirNamedAttribute valueAttr = mlirNamedAttributeGet(
- mlirIdentifierGet(ctx, mlirStringRefCreateFromCString("value")), literal);
- MlirOperationState constState = mlirOperationStateGet(
- mlirStringRefCreateFromCString("arith.constant"), loc);
- MlirType type =
- mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(typeStr));
- mlirOperationStateAddResults(&constState, 1, &type);
- mlirOperationStateAddAttributes(&constState, 1, &valueAttr);
- MlirOperation constOp = mlirOperationCreate(&constState);
- return mlirOperationGetResult(constOp, 0);
-}
-
static void registerAllUpstreamDialects(MlirContext ctx) {
MlirDialectRegistry registry = mlirDialectRegistryCreate();
mlirRegisterAllDialects(registry);
MlirOperation func = mlirOperationCreate(&funcState);
mlirBlockInsertOwnedOperation(moduleBody, 0, func);
- MlirValue constZeroValue = makeConstantLiteral(ctx, "0", "index");
- MlirOperation constZero = mlirOpResultGetOwner(constZeroValue);
+ MlirType indexType =
+ mlirTypeParseGet(ctx, mlirStringRefCreateFromCString("index"));
+ MlirAttribute indexZeroLiteral =
+ mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
+ MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(
+ mlirIdentifierGet(ctx, mlirStringRefCreateFromCString("value")),
+ indexZeroLiteral);
+ MlirOperationState constZeroState = mlirOperationStateGet(
+ mlirStringRefCreateFromCString("arith.constant"), location);
+ mlirOperationStateAddResults(&constZeroState, 1, &indexType);
+ mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
+ MlirOperation constZero = mlirOperationCreate(&constZeroState);
mlirBlockAppendOwnedOperation(funcBody, constZero);
MlirValue funcArg0 = mlirBlockGetArgument(funcBody, 0);
+ MlirValue constZeroValue = mlirOperationGetResult(constZero, 0);
MlirValue dimOperands[] = {funcArg0, constZeroValue};
MlirOperationState dimState = mlirOperationStateGet(
mlirStringRefCreateFromCString("memref.dim"), location);
mlirOperationStateAddOperands(&dimState, 2, dimOperands);
- MlirType indexType =
- mlirTypeParseGet(ctx, mlirStringRefCreateFromCString("index"));
mlirOperationStateAddResults(&dimState, 1, &indexType);
MlirOperation dim = mlirOperationCreate(&dimState);
mlirBlockAppendOwnedOperation(funcBody, dim);
mlirStringRefCreateFromCString("arith.constant"), location);
mlirOperationStateAddResults(&constOneState, 1, &indexType);
mlirOperationStateAddAttributes(&constOneState, 1, &indexOneValueAttr);
- MlirValue constOneValue = makeConstantLiteral(ctx, "1", "index");
- MlirOperation constOne = mlirOpResultGetOwner(constOneValue);
+ MlirOperation constOne = mlirOperationCreate(&constOneState);
mlirBlockAppendOwnedOperation(funcBody, constOne);
MlirValue dimValue = mlirOperationGetResult(dim, 0);
+ MlirValue constOneValue = mlirOperationGetResult(constOne, 0);
MlirValue loopOperands[] = {constZeroValue, dimValue, constOneValue};
MlirOperationState loopState = mlirOperationStateGet(
mlirStringRefCreateFromCString("scf.for"), location);
return 0;
}
+void callbackSetFixedLengthString(const char *data, intptr_t len,
+ void *userData) {
+ strncpy(userData, data, len);
+}
+
bool stringIsEqual(const char *lhs, MlirStringRef rhs) {
if (strlen(lhs) != rhs.length) {
return false;
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("arith"));
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("test"));
MlirLocation loc = mlirLocationUnknownGet(ctx);
+ MlirType indexType = mlirIndexTypeGet(ctx);
// Create some constants to use as operands.
- MlirValue constZeroValue = makeConstantLiteral(ctx, "0", "index");
- MlirValue constOneValue = makeConstantLiteral(ctx, "1", "index");
+ MlirAttribute indexZeroLiteral =
+ mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
+ MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(
+ mlirIdentifierGet(ctx, mlirStringRefCreateFromCString("value")),
+ indexZeroLiteral);
+ MlirOperationState constZeroState = mlirOperationStateGet(
+ mlirStringRefCreateFromCString("arith.constant"), loc);
+ mlirOperationStateAddResults(&constZeroState, 1, &indexType);
+ mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
+ MlirOperation constZero = mlirOperationCreate(&constZeroState);
+ MlirValue constZeroValue = mlirOperationGetResult(constZero, 0);
+
+ MlirAttribute indexOneLiteral =
+ mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("1 : index"));
+ MlirNamedAttribute indexOneValueAttr = mlirNamedAttributeGet(
+ mlirIdentifierGet(ctx, mlirStringRefCreateFromCString("value")),
+ indexOneLiteral);
+ MlirOperationState constOneState = mlirOperationStateGet(
+ mlirStringRefCreateFromCString("arith.constant"), loc);
+ mlirOperationStateAddResults(&constOneState, 1, &indexType);
+ mlirOperationStateAddAttributes(&constOneState, 1, &indexOneValueAttr);
+ MlirOperation constOne = mlirOperationCreate(&constOneState);
+ MlirValue constOneValue = mlirOperationGetResult(constOne, 0);
// Create the operation under test.
mlirContextSetAllowUnregisteredDialects(ctx, true);
return 3;
}
- MlirOperationState op2State =
- mlirOperationStateGet(mlirStringRefCreateFromCString("dummy.op2"), loc);
- MlirValue initialOperands2[] = {constOneValue};
- mlirOperationStateAddOperands(&op2State, 1, initialOperands2);
- MlirOperation op2 = mlirOperationCreate(&op2State);
-
- MlirOpOperand use3 = mlirValueGetFirstUse(constOneValue);
- fprintf(stderr, "First use owner: ");
- mlirOperationPrint(mlirOpOperandGetOwner(use3), printToStderr, NULL);
- fprintf(stderr, "\n");
- // CHECK: First use owner: "dummy.op2"
-
- use3 = mlirOpOperandGetNextUse(mlirValueGetFirstUse(constOneValue));
- fprintf(stderr, "Second use owner: ");
- mlirOperationPrint(mlirOpOperandGetOwner(use3), printToStderr, NULL);
- fprintf(stderr, "\n");
- // CHECK: Second use owner: "dummy.op"
-
- MlirValue constTwoValue = makeConstantLiteral(ctx, "2", "index");
- mlirValueReplaceAllUsesOfWith(constOneValue, constTwoValue);
-
- use3 = mlirValueGetFirstUse(constOneValue);
- if (!mlirOpOperandIsNull(use3)) {
- fprintf(stderr, "ERROR: Use should be null\n");
- return 4;
- }
-
- MlirOpOperand use4 = mlirValueGetFirstUse(constTwoValue);
- fprintf(stderr, "First replacement use owner: ");
- mlirOperationPrint(mlirOpOperandGetOwner(use4), printToStderr, NULL);
- fprintf(stderr, "\n");
- // CHECK: First replacement use owner: "dummy.op"
-
- use4 = mlirOpOperandGetNextUse(mlirValueGetFirstUse(constTwoValue));
- fprintf(stderr, "Second replacement use owner: ");
- mlirOperationPrint(mlirOpOperandGetOwner(use4), printToStderr, NULL);
- fprintf(stderr, "\n");
- // CHECK: Second replacement use owner: "dummy.op2"
-
mlirOperationDestroy(op);
- mlirOperationDestroy(op2);
- mlirOperationDestroy(mlirOpResultGetOwner(constZeroValue));
- mlirOperationDestroy(mlirOpResultGetOwner(constOneValue));
- mlirOperationDestroy(mlirOpResultGetOwner(constTwoValue));
+ mlirOperationDestroy(constZero);
+ mlirOperationDestroy(constOne);
mlirContextDestroy(ctx);
return 0;
registerAllUpstreamDialects(ctx);
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("func"));
+ MlirLocation loc = mlirLocationUnknownGet(ctx);
+ MlirType indexType = mlirIndexTypeGet(ctx);
MlirStringRef valueStringRef = mlirStringRefCreateFromCString("value");
- MlirValue constZeroValue = makeConstantLiteral(ctx, "0", "index");
- MlirOperation constZero = mlirOpResultGetOwner(constZeroValue);
+ MlirAttribute indexZeroLiteral =
+ mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
+ MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(
+ mlirIdentifierGet(ctx, valueStringRef), indexZeroLiteral);
+ MlirOperationState constZeroState = mlirOperationStateGet(
+ mlirStringRefCreateFromCString("arith.constant"), loc);
+ mlirOperationStateAddResults(&constZeroState, 1, &indexType);
+ mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
+ MlirOperation constZero = mlirOperationCreate(&constZeroState);
MlirAttribute indexOneLiteral =
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("1 : index"));
}
MlirLocation loc = mlirLocationUnknownGet(ctx);
+ MlirType indexType = mlirIndexTypeGet(ctx);
+ MlirStringRef valueStringRef = mlirStringRefCreateFromCString("value");
// Create a registered operation, which should have a type id.
- MlirValue constZeroValue = makeConstantLiteral(ctx, "0", "index");
- MlirOperation constZero = mlirOpResultGetOwner(constZeroValue);
+ MlirAttribute indexZeroLiteral =
+ mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
+ MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(
+ mlirIdentifierGet(ctx, valueStringRef), indexZeroLiteral);
+ MlirOperationState constZeroState = mlirOperationStateGet(
+ mlirStringRefCreateFromCString("arith.constant"), loc);
+ mlirOperationStateAddResults(&constZeroState, 1, &indexType);
+ mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
+ MlirOperation constZero = mlirOperationCreate(&constZeroState);
if (!mlirOperationVerify(constZero)) {
fprintf(stderr, "ERROR: Expected operation to verify correctly\n");