Skip to content

Commit

Permalink
chore: more specific repr for Attribute types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziqi-Yang committed Sep 1, 2024
1 parent d7e0e2a commit fce6292
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions example/parse_ir_assmebly.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
@glob_f = global float 1.5
@glob_struct = global %struct.glob_type {i64 0, [2 x i64] [i64 0, i64 0]}
define i32 @sum(i32 nocapture %.1, i32 %.2) noinline {
define zeroext i32 @sum(i32 nocapture %.1, i32 %.2) cold {
%.3 = add i32 %.1, %.2
%.4 = add i32 0, %.3
ret i32 %.4
}
define void @foo() {
define void @foo() "frame-pointer"="all" {
call void asm sideeffect "nop", ""()
ret void
}
Expand All @@ -36,10 +36,10 @@

# TODO it seems we cannot get function attributes (not parameter's)
f_attrs = f.get_attributes_at_index(core.FunctionAttributeIndex)
print(f"Function Attrs: {f_attrs}")
print(f"Function Attributes: {f_attrs}")

ret_attrs = f.get_attributes_at_index(core.ReturnAttributeIndex)
print(f"Return Attrs: {ret_attrs}")
print(f"Return Attributes: {ret_attrs}")

for a in f.args:
print(f'\tArgument | name: "{a.name}", type: "{a.type}"')
Expand Down
18 changes: 18 additions & 0 deletions src/llvm/Core/miscClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,12 @@ void bindOtherClasses(nb::module_ &m) {
new (t) PymEnumAttribute(LLVMCreateEnumAttribute(c.get(), kindID, val));
},
"context"_a, "kind_id"_a, "val"_a)
.def("__repr__",
[](PymAttribute &self) {
using namespace llvm;
Attribute attr = unwrap(self.get());
return fmt::format("<EnumAttribute str='{}'>", attr.getAsString());
})
.def_prop_ro("kind",
[](PymEnumAttribute &attr) {
return LLVMGetEnumAttributeKind(attr.get());
Expand Down Expand Up @@ -1131,6 +1137,12 @@ void bindOtherClasses(nb::module_ &m) {
(context.get(), kind_id, type.get()));
},
"context"_a, "kind_id"_a, "type"_a)
.def("__repr__",
[](PymAttribute &self) {
using namespace llvm;
Attribute attr = unwrap(self.get());
return fmt::format("<TypeAttribute str='{}'>", attr.getAsString());
})
.def_prop_ro("value",
[](PymTypeAttribute &ta){
return PymTypeAuto(LLVMGetTypeAttributeValue(ta.get()));
Expand All @@ -1146,6 +1158,12 @@ void bindOtherClasses(nb::module_ &m) {
new (t) PymStringAttribute(raw);
},
"context"_a, "kind"_a, "value"_a)
.def("__repr__",
[](PymAttribute &self) {
using namespace llvm;
Attribute attr = unwrap(self.get());
return fmt::format("<StringAttribute str='{}'>", attr.getAsString());
})
.def_prop_ro("kind",
[](PymStringAttribute &ta) {
unsigned length;
Expand Down

0 comments on commit fce6292

Please sign in to comment.