From d7e0e2ab051953a9871c2349bf6bc2d8802ac2cb Mon Sep 17 00:00:00 2001 From: meowking Date: Sun, 1 Sep 2024 11:59:58 +0800 Subject: [PATCH] refactor: function attributes --- example/parse_ir_assmebly.py | 8 ++++--- src/llvm/Core/enum.cpp | 40 +++++++++++++++++++++++------------ src/llvm/Core/miscClasses.cpp | 2 +- src/llvm/types_priv.h | 32 ++++++++++++++-------------- 4 files changed, 48 insertions(+), 34 deletions(-) diff --git a/example/parse_ir_assmebly.py b/example/parse_ir_assmebly.py index c229e7d..390e78b 100644 --- a/example/parse_ir_assmebly.py +++ b/example/parse_ir_assmebly.py @@ -16,7 +16,6 @@ ret i32 %.4 } - ; Function Attrs: noinline nounwind optnone uwtable define void @foo() { call void asm sideeffect "nop", ""() ret void @@ -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}"') diff --git a/src/llvm/Core/enum.cpp b/src/llvm/Core/enum.cpp index f28ff64..b881a9d 100644 --- a/src/llvm/Core/enum.cpp +++ b/src/llvm/Core/enum.cpp @@ -375,9 +375,12 @@ void bindEnums(nb::module_ &m) { "However, duplicate entries in the second list are dropped during " "the append operation."); - nb::enum_(m, "AttributeIndex", "AttributeIndex") - .value("Return", PymAttributeIndex::Return) - .value("Function", PymAttributeIndex::Function); + // nb::enum_(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_(m, "TailCallKind", "TailCallKind") .value("LLVMTailCallKindNone", LLVMTailCallKind::LLVMTailCallKindNone) @@ -385,18 +388,27 @@ void bindEnums(nb::module_ &m) { .value("LLVMTailCallKindMustTail", LLVMTailCallKind::LLVMTailCallKindMustTail) .value("LLVMTailCallKindNoTail", LLVMTailCallKind::LLVMTailCallKindNoTail); - // TODO LLVMAttributeIndex - nb::enum_(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_(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 diff --git a/src/llvm/Core/miscClasses.cpp b/src/llvm/Core/miscClasses.cpp index 23c4c66..e7c6e28 100644 --- a/src/llvm/Core/miscClasses.cpp +++ b/src/llvm/Core/miscClasses.cpp @@ -1080,7 +1080,7 @@ void bindOtherClasses(nb::module_ &m) { [](PymAttribute &self) { using namespace llvm; Attribute attr = unwrap(self.get()); - return fmt::format("", attr.getAsString()); + return fmt::format("", attr.getAsString()); }) .def_prop_ro("is_enum", [](PymAttribute &attr) { diff --git a/src/llvm/types_priv.h b/src/llvm/types_priv.h index 44c859e..f3ae67c 100644 --- a/src/llvm/types_priv.h +++ b/src/llvm/types_priv.h @@ -246,22 +246,22 @@ .def("__hash__", &PymLLVMObject::__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