From c71848f961059d7aa5fc67220017227fa3ec8dc3 Mon Sep 17 00:00:00 2001 From: liushuyu Date: Thu, 23 Jan 2025 13:23:24 -0700 Subject: [PATCH] gen: fix control-flow protection attribute marking (#4828) * gen/modules.cpp: use setModuleFlag API to replace ... ... the module attribute to avoid LLVM assertions if specified module attribute already exists * changelog for gen/module.cpp cf-protection fix --- CHANGELOG.md | 1 + gen/modules.cpp | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87a6313c54..04e691309c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ #### Platform support #### Bug fixes +- Building multi-file D applications with control-flow protection will no longer cause LDC to throw an internal compiler error. (#4828) # LDC 1.40.0 (2024-12-15) diff --git a/gen/modules.cpp b/gen/modules.cpp index 67191e1d9b..dce1b59f6f 100644 --- a/gen/modules.cpp +++ b/gen/modules.cpp @@ -391,15 +391,18 @@ void registerModuleInfo(Module *m) { void addModuleFlags(llvm::Module &m) { const auto ModuleMinFlag = llvm::Module::Min; + const auto ConstantOne = + llvm::ConstantInt::get(LLType::getInt32Ty(m.getContext()), 1); + const auto ConstantOneMetadata = llvm::ConstantAsMetadata::get(ConstantOne); if (opts::fCFProtection == opts::CFProtectionType::Return || opts::fCFProtection == opts::CFProtectionType::Full) { - m.addModuleFlag(ModuleMinFlag, "cf-protection-return", 1); + m.setModuleFlag(ModuleMinFlag, "cf-protection-return", ConstantOneMetadata); } if (opts::fCFProtection == opts::CFProtectionType::Branch || opts::fCFProtection == opts::CFProtectionType::Full) { - m.addModuleFlag(ModuleMinFlag, "cf-protection-branch", 1); + m.setModuleFlag(ModuleMinFlag, "cf-protection-branch", ConstantOneMetadata); } }