Skip to content

Commit

Permalink
refactor: function attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziqi-Yang committed Sep 1, 2024
1 parent cc1897a commit d7e0e2a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 34 deletions.
8 changes: 5 additions & 3 deletions example/parse_ir_assmebly.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
ret i32 %.4
}
; Function Attrs: noinline nounwind optnone uwtable
define void @foo() {
call void asm sideeffect "nop", ""()
ret void
Expand All @@ -36,8 +35,11 @@
assert f.kind == core.ValueKind.Function

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

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

for a in f.args:
print(f'\tArgument | name: "{a.name}", type: "{a.type}"')
Expand Down
40 changes: 26 additions & 14 deletions src/llvm/Core/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,28 +375,40 @@ void bindEnums(nb::module_ &m) {
"However, duplicate entries in the second list are dropped during "
"the append operation.");

nb::enum_<PymAttributeIndex>(m, "AttributeIndex", "AttributeIndex")
.value("Return", PymAttributeIndex::Return)
.value("Function", PymAttributeIndex::Function);
// nb::enum_<PymAttributeIndex>(m, "AttributeIndex", "AttributeIndex")
// .value("Return", PymAttributeIndex::Return)
// .value("Function", PymAttributeIndex::Function);

m.attr("ReturnAttributeIndex") = (unsigned int) LLVMAttributeReturnIndex;
m.attr("FunctionAttributeIndex") = (unsigned int) LLVMAttributeFunctionIndex;

nb::enum_<LLVMTailCallKind>(m, "TailCallKind", "TailCallKind")
.value("LLVMTailCallKindNone", LLVMTailCallKind::LLVMTailCallKindNone)
.value("LLVMTailCallKindTail", LLVMTailCallKind::LLVMTailCallKindTail)
.value("LLVMTailCallKindMustTail", LLVMTailCallKind::LLVMTailCallKindMustTail)
.value("LLVMTailCallKindNoTail", LLVMTailCallKind::LLVMTailCallKindNoTail);

// TODO LLVMAttributeIndex

nb::enum_<PymLLVMFastMathFlags>(m, "FastMathFlags", "FastMathFlags")
.value("AllowReassoc", PymLLVMFastMathFlags::AllowReassoc)
.value("NoNaNs", PymLLVMFastMathFlags::NoNaNs)
.value("NoInfs", PymLLVMFastMathFlags::NoInfs)
.value("NoSignedZeros", PymLLVMFastMathFlags::NoSignedZeros)
.value("AllowReciprocal", PymLLVMFastMathFlags::AllowReciprocal)
.value("AllowContract", PymLLVMFastMathFlags::AllowContract)
.value("ApproxFunc", PymLLVMFastMathFlags::ApproxFunc)
.value("None_", PymLLVMFastMathFlags::None)
.value("All", PymLLVMFastMathFlags::All);
// nb::enum_<PymLLVMFastMathFlags>(m, "FastMathFlags", "FastMathFlags")
// .value("AllowReassoc", PymLLVMFastMathFlags::AllowReassoc)
// .value("NoNaNs", PymLLVMFastMathFlags::NoNaNs)
// .value("NoInfs", PymLLVMFastMathFlags::NoInfs)
// .value("NoSignedZeros", PymLLVMFastMathFlags::NoSignedZeros)
// .value("AllowReciprocal", PymLLVMFastMathFlags::AllowReciprocal)
// .value("AllowContract", PymLLVMFastMathFlags::AllowContract)
// .value("ApproxFunc", PymLLVMFastMathFlags::ApproxFunc)
// .value("None_", PymLLVMFastMathFlags::None)
// .value("All", PymLLVMFastMathFlags::All);

m.attr("AllowReassocFMFlag") = (unsigned int) LLVMFastMathAllowReassoc;
m.attr("NoNaNsFMFlag") = (unsigned int) LLVMFastMathNoNaNs;
m.attr("NoInfsFMFlag") = (unsigned int) LLVMFastMathNoInfs;
m.attr("NoSignedZerosFMFlag") = (unsigned int) LLVMFastMathNoSignedZeros;
m.attr("AllowReciprocalFMFlag") = (unsigned int) LLVMFastMathAllowReciprocal;
m.attr("AllowContractFMFlag") = (unsigned int) LLVMFastMathAllowContract;
m.attr("ApproxFuncFMFlag") = (unsigned int) LLVMFastMathApproxFunc;
m.attr("NoneFMFlag") = (unsigned int) LLVMFastMathNone;
m.attr("AllFMFlag") = (unsigned int) LLVMFastMathAll;

// TODO LLVMFastMathFlags

Expand Down
2 changes: 1 addition & 1 deletion src/llvm/Core/miscClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ void bindOtherClasses(nb::module_ &m) {
[](PymAttribute &self) {
using namespace llvm;
Attribute attr = unwrap(self.get());
return fmt::format("<Attribute name='{}'>", attr.getAsString());
return fmt::format("<Attribute str='{}'>", attr.getAsString());
})
.def_prop_ro("is_enum",
[](PymAttribute &attr) {
Expand Down
32 changes: 16 additions & 16 deletions src/llvm/types_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,22 +246,22 @@
.def("__hash__", &PymLLVMObject<ClassName, UnderlyingType>::__hash__);


enum class PymAttributeIndex {
Return = LLVMAttributeReturnIndex,
Function = LLVMAttributeFunctionIndex
};

enum class PymLLVMFastMathFlags {
AllowReassoc = LLVMFastMathAllowReassoc,
NoNaNs = LLVMFastMathNoNaNs,
NoInfs = LLVMFastMathNoInfs,
NoSignedZeros = LLVMFastMathNoSignedZeros,
AllowReciprocal = LLVMFastMathAllowReciprocal,
AllowContract = LLVMFastMathAllowContract,
ApproxFunc = LLVMFastMathApproxFunc,
None = LLVMFastMathNone,
All = LLVMFastMathAll
};
// enum class PymAttributeIndex {
// Return = LLVMAttributeReturnIndex,
// Function = LLVMAttributeFunctionIndex
// };

// enum class PymLLVMFastMathFlags {
// AllowReassoc = LLVMFastMathAllowReassoc,
// NoNaNs = LLVMFastMathNoNaNs,
// NoInfs = LLVMFastMathNoInfs,
// NoSignedZeros = LLVMFastMathNoSignedZeros,
// AllowReciprocal = LLVMFastMathAllowReciprocal,
// AllowContract = LLVMFastMathAllowContract,
// ApproxFunc = LLVMFastMathApproxFunc,
// None = LLVMFastMathNone,
// All = LLVMFastMathAll
// };

/*
* Check three places: here, class inheritance, binding class inheritance
Expand Down

0 comments on commit d7e0e2a

Please sign in to comment.