Skip to content

Commit

Permalink
[compiler] Remove need for passing Module to NameMangler
Browse files Browse the repository at this point in the history
This was only used for its LLVMContext, which we already provide to the
mangler upon construction - perhaps for legacy reasons.
  • Loading branch information
frasercrmck committed Jul 4, 2023
1 parent 348ead2 commit 55e952b
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using namespace refsi_g1_wi;

StructType *RefSiG1BIMuxInfo::getExecStateStruct(Module &M) {
static constexpr const char *StructName = "exec_state";
if (auto *ty = multi_llvm::getStructTypeByName(M, StructName)) {
if (auto *ty = multi_llvm::getStructTypeByName(M.getContext(), StructName)) {
return ty;
}

Expand Down
4 changes: 2 additions & 2 deletions modules/compiler/multi_llvm/include/multi_llvm/multi_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ inline llvm::InlineResult InlineFunction(llvm::CallInst *CI,
#endif
}

inline llvm::StructType *getStructTypeByName(llvm::Module &module,
inline llvm::StructType *getStructTypeByName(llvm::LLVMContext &ctx,
llvm::StringRef name) {
return llvm::StructType::getTypeByName(module.getContext(), name);
return llvm::StructType::getTypeByName(ctx, name);
}

inline llvm::DILocation *getDILocation(unsigned Line, unsigned Column,
Expand Down
2 changes: 1 addition & 1 deletion modules/compiler/source/base/source/spir_fixup_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ PreservedAnalyses compiler::spir::SpirFixupPass::run(llvm::Module &M,
// done so
if (nullptr == SamplerTypePtr) {
auto *samplerType =
multi_llvm::getStructTypeByName(M, "opencl.sampler_t");
multi_llvm::getStructTypeByName(M.getContext(), "opencl.sampler_t");

if (nullptr == samplerType) {
samplerType = StructType::create(M.getContext(), "opencl.sampler_t");
Expand Down
2 changes: 1 addition & 1 deletion modules/compiler/spirv-ll/source/builder_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ cargo::optional<Error> Builder::create<OpTypeImage>(const OpTypeImage *op) {
// llvm::Context, creating a new StructType when one already exists with the
// same name results in .1 being appended to the struct name causing issues.
auto *namedTy =
multi_llvm::getStructTypeByName(*module.llvmModule, imageTypeName);
multi_llvm::getStructTypeByName(*context.llvmContext, imageTypeName);
if (namedTy) {
structTy = namedTy;
} else {
Expand Down
6 changes: 4 additions & 2 deletions modules/compiler/targets/host/source/HostMuxBuiltinInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ enum {

StructType *HostBIMuxInfo::getMiniWGInfoStruct(Module &M) {
static constexpr const char *HostStructName = "MiniWGInfo";
if (auto *ty = multi_llvm::getStructTypeByName(M, HostStructName)) {
if (auto *ty =
multi_llvm::getStructTypeByName(M.getContext(), HostStructName)) {
return ty;
}

Expand All @@ -52,7 +53,8 @@ StructType *HostBIMuxInfo::getMiniWGInfoStruct(Module &M) {

StructType *HostBIMuxInfo::getScheduleInfoStruct(Module &M) {
static constexpr const char *HostStructName = "Mux_schedule_info_s";
if (auto *ty = multi_llvm::getStructTypeByName(M, HostStructName)) {
if (auto *ty =
multi_llvm::getStructTypeByName(M.getContext(), HostStructName)) {
return ty;
}
auto &Ctx = M.getContext();
Expand Down
7 changes: 3 additions & 4 deletions modules/compiler/test/mangling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
using namespace compiler::utils;

struct ManglingTest : ::testing::Test {
void SetUp() override { M = std::make_unique<llvm::Module>("ID", Context); }
void SetUp() override {}

std::unique_ptr<llvm::Module> parseModule(llvm::StringRef Assembly) {
llvm::SMDiagnostic Error;
Expand All @@ -47,7 +47,6 @@ struct ManglingTest : ::testing::Test {
}

llvm::LLVMContext Context;
std::unique_ptr<llvm::Module> M;
};

TEST_F(ManglingTest, MangleBuiltinTypes) {
Expand All @@ -61,7 +60,7 @@ TEST_F(ManglingTest, MangleBuiltinTypes) {
#if LLVM_VERSION_LESS(17, 0)
GTEST_SKIP();
#else
NameMangler Mangler(&Context, M.get());
NameMangler Mangler(&Context);

std::pair<llvm::Type *, const char *> TypesToMangle[] = {
{tgtext::getEventTy(Context), "9ocl_event"},
Expand Down Expand Up @@ -103,7 +102,7 @@ TEST_F(ManglingTest, DemangleImage1DTy) {
declare void @_Z4test11ocl_image1d(ptr %img)
)");

NameMangler Mangler(&Context, M.get());
NameMangler Mangler(&Context);

auto *F = M->getFunction("_Z4test11ocl_image1d");
EXPECT_TRUE(F);
Expand Down
8 changes: 1 addition & 7 deletions modules/compiler/utils/include/compiler/utils/mangling.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

namespace llvm {
class LLVMContext;
class Module;
class Type;
class raw_ostream;
} // namespace llvm
Expand Down Expand Up @@ -230,8 +229,7 @@ class NameMangler final {
/// @brief Create a new name mangler.
///
/// @param[in] context LLVM context to use.
/// @param[in] module LLVM module to use.
NameMangler(llvm::LLVMContext *context, llvm::Module *module = nullptr);
NameMangler(llvm::LLVMContext *context);

/// @brief Determine the mangled name of a function.
///
Expand Down Expand Up @@ -312,8 +310,6 @@ class NameMangler final {
/// @return Demangled name or original name if not mangled.
llvm::StringRef demangleName(llvm::StringRef Name);

void setModule(llvm::Module *m) { M = m; };

private:
/// @brief Try to mangle the given qualified type. This only works for simple
/// types that do not require string manipulation.
Expand Down Expand Up @@ -405,8 +401,6 @@ class NameMangler final {

/// @brief LLVM context used to access LLVM types.
llvm::LLVMContext *Context;
/// @brief LLVM mdoule used to check existing LLVM named struct types.
llvm::Module *M;
};
} // namespace utils
} // namespace compiler
Expand Down
2 changes: 1 addition & 1 deletion modules/compiler/utils/source/cl_builtin_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ Function *CLBuiltinInfo::getVectorEquivalent(Builtin const &B, unsigned Width,

// Builtin functions have mangled names. If it's not mangled, there will be
// no vector equivalent.
NameMangler Mangler(&B.function.getContext(), M);
NameMangler Mangler(&B.function.getContext());
SmallVector<Type *, 4> BuiltinArgTypes, BuiltinPointeeTypes;
SmallVector<TypeQualifiers, 4> BuiltinArgQuals;
StringRef BuiltinName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ std::string lookupWGBuiltin(StringRef SubgroupBuiltin, LLVMContext *Ctx,
return compiler::utils::MuxBuiltins::work_group_barrier;
}

compiler::utils::NameMangler Mangler(Ctx, M);
compiler::utils::NameMangler Mangler(Ctx);
SmallVector<Type *, 4> ArgumentTypes;
SmallVector<compiler::utils::TypeQualifiers, 4> Qualifiers;

Expand Down
4 changes: 2 additions & 2 deletions modules/compiler/utils/source/dma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ void buildThreadCheck(llvm::BasicBlock *entryBlock, llvm::BasicBlock *trueBlock,
}

llvm::StructType *getOrCreateMuxDMAEventType(llvm::Module &m) {
if (auto *eventType =
multi_llvm::getStructTypeByName(m, MuxBuiltins::dma_event_type)) {
if (auto *eventType = multi_llvm::getStructTypeByName(
m.getContext(), MuxBuiltins::dma_event_type)) {
return eventType;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/compiler/utils/source/group_collective_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ llvm::Constant *compiler::utils::getIdentityVal(RecurKind Kind, Type *Ty) {

multi_llvm::Optional<compiler::utils::GroupCollective>
compiler::utils::isGroupCollective(llvm::Function *f) {
compiler::utils::NameMangler Mangler(&f->getContext(), f->getParent());
compiler::utils::NameMangler Mangler(&f->getContext());
SmallVector<Type *, 4> ArgumentTypes;
SmallVector<compiler::utils::TypeQualifiers, 4> Qualifiers;

Expand Down
2 changes: 1 addition & 1 deletion modules/compiler/utils/source/link_builtins_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const StringSet<> DeferredBuiltins = {
};

bool isDeferredBuiltin(Function &F) {
compiler::utils::NameMangler Mangler(&F.getContext(), F.getParent());
compiler::utils::NameMangler Mangler(&F.getContext());

SmallVector<Type *, 4> Types;
SmallVector<compiler::utils::TypeQualifiers, 4> Quals;
Expand Down
8 changes: 4 additions & 4 deletions modules/compiler/utils/source/mangling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ namespace compiler {
namespace utils {
using namespace llvm;

NameMangler::NameMangler(LLVMContext *context, Module *module)
: Context(context), M(module) {}
NameMangler::NameMangler(LLVMContext *context) : Context(context) {}

std::string NameMangler::mangleName(StringRef Name, ArrayRef<Type *> Tys,
ArrayRef<TypeQualifiers> Quals) {
Expand Down Expand Up @@ -498,10 +497,11 @@ bool NameMangler::demangleOpenCLBuiltinType(Lexer &L, Type *&Ty) {
return false;
}

if (auto *const OpenCLType = multi_llvm::getStructTypeByName(*M, Name)) {
if (auto *const OpenCLType =
multi_llvm::getStructTypeByName(*Context, Name)) {
Ty = OpenCLType;
} else {
Ty = llvm::StructType::create(M->getContext(), Name);
Ty = llvm::StructType::create(*Context, Name);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ OptimalBuiltinReplacementPass::OptimalBuiltinReplacementPass() {
Value *OptimalBuiltinReplacementPass::replaceBuiltinWithInlineIR(
CallBase &CB) const {
auto *M = CB.getModule();
LLVMContext &Ctx = M->getContext();
NameMangler mangler(&Ctx, M);
NameMangler mangler(&M->getContext());

SmallVector<Type *, 4> Types;
SmallVector<TypeQualifiers, 4> Quals;
Expand Down
3 changes: 1 addition & 2 deletions modules/compiler/utils/source/replace_async_copies_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,7 @@ void defineWaitGroupEvents(Function &WaitGroupEvents) {
/// @retval True if @p Function is async builtin and is now defined.
/// @retval False if @p Function was not async builtin.
bool runOnFunction(Function &Function) {
compiler::utils::NameMangler Mangler(&Function.getContext(),
Function.getParent());
compiler::utils::NameMangler Mangler(&Function.getContext());
// Parse the name part.
StringRef DemangledName = Mangler.demangleName(Function.getName());

Expand Down
4 changes: 2 additions & 2 deletions modules/compiler/utils/source/scheduling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static constexpr const char *WorkGroupParamName = "MuxWorkGroupInfo";
StructType *getWorkItemInfoStructTy(llvm::Module &M) {
LLVMContext &ctx = M.getContext();
// Check whether this struct has previously been defined.
if (auto *ty = multi_llvm::getStructTypeByName(M, WorkItemParamName)) {
if (auto *ty = multi_llvm::getStructTypeByName(ctx, WorkItemParamName)) {
return ty;
}
auto *uint_type = Type::getInt32Ty(ctx);
Expand All @@ -56,7 +56,7 @@ StructType *getWorkItemInfoStructTy(llvm::Module &M) {
StructType *getWorkGroupInfoStructTy(llvm::Module &M) {
LLVMContext &ctx = M.getContext();
// Check whether this struct has previously been defined.
if (auto *ty = multi_llvm::getStructTypeByName(M, WorkGroupParamName)) {
if (auto *ty = multi_llvm::getStructTypeByName(ctx, WorkGroupParamName)) {
return ty;
}
auto *uint_type = Type::getInt32Ty(ctx);
Expand Down
4 changes: 2 additions & 2 deletions modules/compiler/utils/source/unique_opaque_structs_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ static compiler::utils::StructMap uniqueOpaqueSuffixedStructs(

// Check whether there is a type in the context with the same name minus a
// suffix.
if (auto *ctxStructTy =
multi_llvm::getStructTypeByName(module, structName.rtrim(Suffix))) {
if (auto *ctxStructTy = multi_llvm::getStructTypeByName(
module.getContext(), structName.rtrim(Suffix))) {
// Make sure it is also opaque.
if (!ctxStructTy->isOpaque()) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion modules/compiler/vecz/source/vectorization_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ VectorizationResult &VectorizationContext::getOrCreateBuiltin(
return result;
}

compiler::utils::NameMangler Mangler(&F.getContext(), &Module);
compiler::utils::NameMangler Mangler(&F.getContext());
auto const BuiltinName = Mangler.demangleName(F.getName()).str();

result.func = VectorCallee;
Expand Down

0 comments on commit 55e952b

Please sign in to comment.