diff --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h --- a/mlir/include/mlir-c/IR.h +++ b/mlir/include/mlir-c/IR.h @@ -776,6 +776,13 @@ MLIR_CAPI_EXPORTED void mlirValuePrint(MlirValue value, MlirStringCallback callback, void *userData); +/// Prints a value as an operand (i.e., the ValueID). Note, this isn't +/// canonical (see comment in AsmPrinter::Value::printAsOperand. +MLIR_CAPI_EXPORTED void mlirValuePrintAsOperand(MlirValue value, + bool shouldUseLocalScope, + MlirStringCallback callback, + void *userData); + /// Returns an op operand representing the first use of the value, or a null op /// operand if there are no uses. MLIR_CAPI_EXPORTED MlirOpOperand mlirValueGetFirstUse(MlirValue value); diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -156,6 +156,14 @@ equivalent to printing the operation that produced it. )"; +static const char kValuePrintAsOperand[] = + R"(Returns the string form of value as an operand (i.e., the ValueID). + +Note, this isn't canonical; region arguments can be shadowed when printing the +main operation. If the IR hasn't been printed, this will produce the old SSA +name and not the shadowed name. +)"; + static const char kValueReplaceAllUsesWithDocstring[] = R"(Replace all uses of value with the new value, updating anything in the IR that uses 'self' to use the other value instead. @@ -3336,6 +3344,16 @@ return printAccum.join(); }, kValueDunderStrDocstring) + .def( + "sprint_as_operand", + [](PyValue &self, bool shouldUseLocalScope) { + PyPrintAccumulator printAccum; + mlirValuePrintAsOperand(self.get(), shouldUseLocalScope, + printAccum.getCallback(), + printAccum.getUserData()); + return printAccum.join(); + }, + py::arg("should_use_local_scope") = false, kValuePrintAsOperand) .def_property_readonly("type", [](PyValue &self) { return PyType( diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp --- a/mlir/lib/CAPI/IR/IR.cpp +++ b/mlir/lib/CAPI/IR/IR.cpp @@ -767,6 +767,37 @@ unwrap(value).print(stream); } +void mlirValuePrintAsOperand(MlirValue value, bool shouldUseLocalScope, + MlirStringCallback callback, void *userData) { + detail::CallbackOstream stream(callback, userData); + + Value cppValue = unwrap(value); + AsmState *state; + if (cppValue.isa()) { + Operation *op = cppValue.cast().getOwner(); + do { + // If we are printing local scope, stop at the first operation that is + // isolated from above. + if (shouldUseLocalScope && op->hasTrait()) + break; + + // Otherwise, traverse up to the next parent. + Operation *parentOp = op->getParentOp(); + if (!parentOp) + break; + op = parentOp; + } while (true); + state = new AsmState(op); + } else if (cppValue.isa()) { + Operation *op = cppValue.cast().getOwner()->getParentOp(); + state = new AsmState(op); + } else { + state = new AsmState(cppValue.getContext()); + } + + cppValue.printAsOperand(stream, *state); +} + MlirOpOperand mlirValueGetFirstUse(MlirValue value) { Value cppValue = unwrap(value); if (cppValue.use_empty()) diff --git a/mlir/test/python/ir/value.py b/mlir/test/python/ir/value.py --- a/mlir/test/python/ir/value.py +++ b/mlir/test/python/ir/value.py @@ -2,6 +2,7 @@ import gc from mlir.ir import * +from mlir.dialects import func def run(f): @@ -90,6 +91,7 @@ assert hash(block.arguments[0]) == hash(op.operands[0]) assert hash(op.result) == hash(ret.operands[0]) + # CHECK-LABEL: TEST: testValueUses @run def testValueUses(): @@ -112,6 +114,7 @@ print(f"Use owner: {use.owner}") print(f"Use operand_number: {use.operand_number}") + # CHECK-LABEL: TEST: testValueReplaceAllUsesWith @run def testValueReplaceAllUsesWith(): @@ -137,3 +140,49 @@ assert use.owner in [op1, op2] print(f"Use owner: {use.owner}") print(f"Use operand_number: {use.operand_number}") + + +# CHECK-LABEL: TEST: testValuePrintAsOperand +@run +def testValuePrintAsOperand(): + ctx = Context() + ctx.allow_unregistered_dialects = True + with Location.unknown(ctx): + i32 = IntegerType.get_signless(32) + module = Module.create() + with InsertionPoint(module.body): + value = Operation.create("custom.op1", results=[i32]).results[0] + # CHECK: Value(%[[VAL1:.*]] = "custom.op1"() : () -> i32) + print(value) + + value2 = Operation.create("custom.op2", results=[i32]).results[0] + # CHECK: Value(%[[VAL2:.*]] = "custom.op2"() : () -> i32) + print(value2) + + f = func.FuncOp("test", ([i32], [])) + entry_block = Block.create_at_start(f.operation.regions[0], [i32, i32]) + + with InsertionPoint(entry_block): + value3 = Operation.create("custom.op3", results=[i32]).results[0] + # CHECK: Value(%[[VAL3:.*]] = "custom.op3"() : () -> i32) + print(value3) + func.ReturnOp([]) + + print(module) + # CHECK: %[[VAL1]] + print(value.sprint_as_operand()) + # CHECK: %[[VAL2]] + print(value2.sprint_as_operand()) + # CHECK: %[[VAL3]] + print(value3.sprint_as_operand()) + # CHECK: %0 + print(value3.sprint_as_operand(should_use_local_scope=True)) + + value2.owner.detach_from_parent() + # CHECK: %0 + print(value2.sprint_as_operand()) + + # CHECK: %arg0 + print(entry_block.arguments[0].sprint_as_operand()) + # CHECK: %arg1 + print(entry_block.arguments[1].sprint_as_operand())