Skip to content

Commit 91dd9b5

Browse files
Apply suggestions
1 parent c4bbfb5 commit 91dd9b5

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

mlir/python/mlir/dialects/gpu/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ def __init__(
7676
super().__init__(function_type, *args, loc=loc, ip=ip, **kwargs)
7777

7878
if isinstance(sym_name, str):
79-
sym_name = StringAttr.get(str(sym_name))
80-
if sym_name is not None:
79+
self.attributes[self.SYM_NAME_ATTR_NAME] = StringAttr.get(sym_name)
80+
elif isinstance(sym_name, StringAttr):
8181
self.attributes[self.SYM_NAME_ATTR_NAME] = sym_name
82+
else:
83+
raise ValueError(
84+
"sym_name must be a string or a StringAttr"
85+
)
8286

8387
if kernel:
8488
self.attributes[self.KERNEL_ATTR_NAME] = UnitAttr.get()
89+
8590
if known_block_size is not None:
8691
if isinstance(known_block_size, Sequence):
8792
self.attributes[self.KNOWN_BLOCK_SIZE_ATTR_NAME] = (
@@ -119,6 +124,9 @@ def is_kernel(self) -> bool:
119124
return self.KERNEL_ATTR_NAME in self.attributes
120125

121126
def add_entry_block(self) -> Block:
127+
if len(self.body.blocks) > 0:
128+
raise RuntimeError(f"Entry block already exists for {self.name.value}")
129+
122130
function_type = self.function_type.value
123131
return self.body.blocks.append(
124132
*function_type.inputs,
@@ -127,6 +135,11 @@ def add_entry_block(self) -> Block:
127135

128136
@property
129137
def entry_block(self) -> Block:
138+
if len(self.body.blocks) == 0:
139+
raise RuntimeError(
140+
f"Entry block does not exist for {self.name.value}." +
141+
" Do you need to call the add_entry_block() method on this GPUFuncOp?"
142+
)
130143
return self.body.blocks[0]
131144

132145
@property

mlir/test/python/dialects/gpu/dialect.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,23 @@ def builder(func: gpu.GPUFuncOp) -> None:
8888
func = gpu.GPUFuncOp(type_attr, name)
8989
func.attributes["sym_name"] = name
9090
func.attributes["gpu.kernel"] = UnitAttr.get()
91-
block = func.body.blocks.append()
91+
92+
try:
93+
func.entry_block
94+
assert False, "Expected RuntimeError"
95+
except RuntimeError as e:
96+
assert str(e) == "Entry block does not exist for kernel0. Do you need to call the add_entry_block() method on this GPUFuncOp?"
97+
98+
block = func.add_entry_block()
9299
with InsertionPoint(block):
93100
builder(func)
94101

102+
try:
103+
func.add_entry_block()
104+
assert False, "Expected RuntimeError"
105+
except RuntimeError as e:
106+
assert str(e) == "Entry block already exists for kernel0"
107+
95108
func = gpu.GPUFuncOp(
96109
func_type,
97110
sym_name="kernel1",

0 commit comments

Comments
 (0)