From 6a2a889b8a2cfee207f77cfdc525a8315cfaf6d7 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Sun, 2 Sep 2018 01:28:26 +0000 Subject: [PATCH] [ORC] Tidy up JITSymbolFlags to remove the need for some explicit static_casts. Removes the implicit conversion to the underlying type for JITSymbolFlags::FlagNames and replaces it with some bitwise and comparison operators. llvm-svn: 341282 --- llvm/include/llvm/ExecutionEngine/JITSymbol.h | 47 ++++++++++++++++++---- .../ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h | 3 +- llvm/lib/ExecutionEngine/Orc/Core.cpp | 6 +-- .../unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp | 3 +- .../ExecutionEngine/Orc/LegacyAPIInteropTest.cpp | 6 +-- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/llvm/include/llvm/ExecutionEngine/JITSymbol.h b/llvm/include/llvm/ExecutionEngine/JITSymbol.h index b26ab46..cf25120 100644 --- a/llvm/include/llvm/ExecutionEngine/JITSymbol.h +++ b/llvm/include/llvm/ExecutionEngine/JITSymbol.h @@ -23,6 +23,7 @@ #include #include +#include "llvm/ADT/BitmaskEnum.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" @@ -54,7 +55,8 @@ public: Exported = 1U << 4, Callable = 1U << 5, Lazy = 1U << 6, - Materializing = 1U << 7 + Materializing = 1U << 7, + LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Materializing) }; static JITSymbolFlags stripTransientFlags(JITSymbolFlags Orig) { @@ -72,6 +74,26 @@ public: JITSymbolFlags(FlagNames Flags, TargetFlagsType TargetFlags) : Flags(Flags), TargetFlags(TargetFlags) {} + /// Implicitly convert to bool. Returs true if any flag is set. + explicit operator bool() const { return Flags != None || TargetFlags != 0; } + + /// Compare for equality. + bool operator==(const JITSymbolFlags &RHS) const { + return Flags == RHS.Flags && TargetFlags == RHS.TargetFlags; + } + + /// Bitwise AND-assignment for FlagNames. + JITSymbolFlags& operator&=(const FlagNames &RHS) { + Flags &= RHS; + return *this; + } + + /// Bitwise OR-assignment for FlagNames. + JITSymbolFlags& operator|=(const FlagNames &RHS) { + Flags |= RHS; + return *this; + } + /// Return true if there was an error retrieving this symbol. bool hasError() const { return (Flags & HasError) == HasError; @@ -113,11 +135,8 @@ public: /// Returns true if the given symbol is known to be callable. bool isCallable() const { return (Flags & Callable) == Callable; } - /// Implicitly convert to the underlying flags type. - operator UnderlyingType&() { return Flags; } - - /// Implicitly convert to the underlying flags type. - operator const UnderlyingType&() const { return Flags; } + /// Get the underlying flags value as an integer. + UnderlyingType getRawFlagsValue() const { return static_cast(Flags); } /// Return a reference to the target-specific flags. TargetFlagsType& getTargetFlags() { return TargetFlags; } @@ -135,10 +154,24 @@ public: fromObjectSymbol(const object::SymbolRef &Symbol); private: - UnderlyingType Flags = None; + FlagNames Flags = None; TargetFlagsType TargetFlags = 0; }; +inline JITSymbolFlags operator&(const JITSymbolFlags &LHS, + const JITSymbolFlags::FlagNames &RHS) { + JITSymbolFlags Tmp = LHS; + Tmp &= RHS; + return Tmp; +} + +inline JITSymbolFlags operator|(const JITSymbolFlags &LHS, + const JITSymbolFlags::FlagNames &RHS) { + JITSymbolFlags Tmp = LHS; + Tmp |= RHS; + return Tmp; +} + /// ARM-specific JIT symbol flags. /// FIXME: This should be moved into a target-specific header. class ARMJITSymbolFlags { diff --git a/llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h b/llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h index bc0da0f9..8db9e31 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h @@ -87,8 +87,7 @@ class SerializationTraits { public: static Error serialize(ChannelT &C, const JITSymbolFlags &Flags) { - return serializeSeq(C, static_cast(Flags), - Flags.getTargetFlags()); + return serializeSeq(C, Flags.getRawFlagsValue(), Flags.getTargetFlags()); } static Error deserialize(ChannelT &C, JITSymbolFlags &Flags) { diff --git a/llvm/lib/ExecutionEngine/Orc/Core.cpp b/llvm/lib/ExecutionEngine/Orc/Core.cpp index 46c1673..0a44e2e 100644 --- a/llvm/lib/ExecutionEngine/Orc/Core.cpp +++ b/llvm/lib/ExecutionEngine/Orc/Core.cpp @@ -1158,8 +1158,7 @@ void JITDylib::emit(const SymbolFlagsMap &Emitted) { assert(DependantJD.Symbols.count(DependantName) && "Dependant has no entry in the Symbols table"); auto &DependantSym = DependantJD.Symbols[DependantName]; - DependantSym.setFlags(static_cast( - DependantSym.getFlags() & ~JITSymbolFlags::Materializing)); + DependantSym.setFlags(DependantSym.getFlags() & ~JITSymbolFlags::Materializing); DependantJD.MaterializingInfos.erase(DependantMII); } } @@ -1177,8 +1176,7 @@ void JITDylib::emit(const SymbolFlagsMap &Emitted) { assert(Symbols.count(Name) && "Symbol has no entry in the Symbols table"); auto &Sym = Symbols[Name]; - Sym.setFlags(static_cast( - Sym.getFlags() & ~JITSymbolFlags::Materializing)); + Sym.setFlags(Sym.getFlags() & ~JITSymbolFlags::Materializing); MaterializingInfos.erase(MII); } } diff --git a/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp b/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp index 24c5378..796736e 100644 --- a/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp +++ b/llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp @@ -533,8 +533,7 @@ TEST_F(CoreAPIsStandardTest, AddAndMaterializeLazySymbol) { TEST_F(CoreAPIsStandardTest, TestBasicWeakSymbolMaterialization) { // Test that weak symbols are materialized correctly when we look them up. - BarSym.setFlags(static_cast(BarSym.getFlags() | - JITSymbolFlags::Weak)); + BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak); bool BarMaterialized = false; auto MU1 = llvm::make_unique( diff --git a/llvm/unittests/ExecutionEngine/Orc/LegacyAPIInteropTest.cpp b/llvm/unittests/ExecutionEngine/Orc/LegacyAPIInteropTest.cpp index 50152cc..36cb710 100644 --- a/llvm/unittests/ExecutionEngine/Orc/LegacyAPIInteropTest.cpp +++ b/llvm/unittests/ExecutionEngine/Orc/LegacyAPIInteropTest.cpp @@ -19,8 +19,7 @@ class LegacyAPIsStandardTest : public CoreAPIsBasedStandardTest {}; namespace { TEST_F(LegacyAPIsStandardTest, TestLambdaSymbolResolver) { - BarSym.setFlags(static_cast(BarSym.getFlags() | - JITSymbolFlags::Weak)); + BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak); cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}}))); @@ -73,8 +72,7 @@ TEST_F(LegacyAPIsStandardTest, TestLambdaSymbolResolver) { TEST_F(LegacyAPIsStandardTest, LegacyLookupHelpersFn) { bool BarMaterialized = false; - BarSym.setFlags(static_cast(BarSym.getFlags() | - JITSymbolFlags::Weak)); + BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak); auto LegacyLookup = [&](const std::string &Name) -> JITSymbol { if (Name == "foo") -- 2.7.4