From 960195fbf4a60ba09c8745c0519ed56d62109181 Mon Sep 17 00:00:00 2001 From: Hideto Ueno Date: Thu, 21 Sep 2023 19:47:29 +0900 Subject: [PATCH] [HW] Fix ModuleType::getInputType to return InOutType for InOut port (#6162) `ModuleType::getInputTypes` returns inout types for inout ports but `getInputType` doesn't wrap with inout. This is inconsistent and python API `module_type.input_types` returns a wrong type for this. --- integration_test/Bindings/Python/dialects/hw.py | 9 +++++---- lib/Dialect/HW/HWTypes.cpp | 5 ++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/integration_test/Bindings/Python/dialects/hw.py b/integration_test/Bindings/Python/dialects/hw.py index 4ec319d2ea65..b7266d6efef5 100644 --- a/integration_test/Bindings/Python/dialects/hw.py +++ b/integration_test/Bindings/Python/dialects/hw.py @@ -124,16 +124,17 @@ def build(module): ports = [ hw.ModulePort(StringAttr.get("out"), i1, hw.ModulePortDirection.OUTPUT), hw.ModulePort(StringAttr.get("in1"), i2, hw.ModulePortDirection.INPUT), - hw.ModulePort(StringAttr.get("in2"), i32, hw.ModulePortDirection.INPUT) + hw.ModulePort(StringAttr.get("in2"), i32, hw.ModulePortDirection.INPUT), + hw.ModulePort(StringAttr.get("in3"), i32, hw.ModulePortDirection.INOUT) ] module_type = hw.ModuleType.get(ports) - # CHECK: !hw.modty + # CHECK: !hw.modty print(module_type) - # CHECK-NEXT: [IntegerType(i2), IntegerType(i32)] + # CHECK-NEXT: [IntegerType(i2), IntegerType(i32), Type(!hw.inout)] print(module_type.input_types) # CHECK-NEXT: [IntegerType(i1)] print(module_type.output_types) - # CHECK-NEXT: ['in1', 'in2'] + # CHECK-NEXT: ['in1', 'in2', 'in3'] print(module_type.input_names) # CHECK-NEXT: ['out'] print(module_type.output_names) diff --git a/lib/Dialect/HW/HWTypes.cpp b/lib/Dialect/HW/HWTypes.cpp index 3445d4503878..b71a9cb03323 100644 --- a/lib/Dialect/HW/HWTypes.cpp +++ b/lib/Dialect/HW/HWTypes.cpp @@ -830,7 +830,10 @@ SmallVector ModuleType::getPortTypes() { } Type ModuleType::getInputType(size_t idx) { - return getPorts()[getPortIdForInputId(idx)].type; + const auto &portInfo = getPorts()[getPortIdForInputId(idx)]; + if (portInfo.dir != ModulePort::InOut) + return portInfo.type; + return InOutType::get(portInfo.type); } Type ModuleType::getOutputType(size_t idx) {