-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for self referential metadata #895
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,22 +42,32 @@ def _fix_di_operands(self, operands): | |
fixed_ops.append((name, op)) | ||
return fixed_ops | ||
|
||
def add_metadata(self, operands): | ||
def add_metadata(self, operands, *, self_ref=False): | ||
""" | ||
Add an unnamed metadata to the module with the given *operands* | ||
(a sequence of values) or return a previous equivalent metadata. | ||
A MDValue instance is returned, it can then be associated to | ||
e.g. an instruction. | ||
|
||
A self referential metadata entry has itself as the first | ||
operand to ensure uniquencess. These references are never | ||
cached. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add few words to relate this to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I extended the description. |
||
""" | ||
if not isinstance(operands, (list, tuple)): | ||
raise TypeError("expected a list or tuple of metadata values, " | ||
"got %r" % (operands,)) | ||
operands = self._fix_metadata_operands(operands) | ||
key = tuple(operands) | ||
if key not in self._metadatacache: | ||
if self_ref or key not in self._metadatacache: | ||
n = len(self.metadata) | ||
md = values.MDValue(self, operands, name=str(n)) | ||
self._metadatacache[key] = md | ||
md = values.MDValue( | ||
self, | ||
operands, | ||
name=str(n), | ||
self_ref=self_ref, | ||
) | ||
if not self_ref: | ||
self._metadatacache[key] = md | ||
else: | ||
md = self._metadatacache[key] | ||
return md | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,6 +323,17 @@ def test_unnamed_metadata_3(self): | |
self.assert_ir_line('!1 = !{ i32 789 }', mod) | ||
self.assert_ir_line('!2 = !{ i32 123, !0, !1, !0 }', mod) | ||
|
||
def test_self_relf_metadata(self): | ||
mod = self.module() | ||
value = mod.add_metadata((), self_ref=True) | ||
self.assert_ir_line('!0 = !{ !0 }', mod) | ||
value2 = mod.add_metadata((value,)) | ||
assert value is not value2 | ||
self.assert_ir_line('!1 = !{ !0 }', mod) | ||
value3 = mod.add_metadata((), self_ref=True) | ||
self.assert_ir_line('!2 = !{ !2 }', mod) | ||
assert value is not value3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add test for metadata that has other operands? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
def test_metadata_string(self): | ||
# Escaping contents of a metadata string | ||
mod = self.module() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed