From 711ad61666573f372e4c2c6f7ec414b1eaa53d42 Mon Sep 17 00:00:00 2001 From: zhengweiwei Date: Wed, 12 Feb 2025 14:42:26 +0800 Subject: [PATCH] Deduplicate strings in the _hilog_ section Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/IB8QCC Signed-off-by: zhengweiwei --- clang/lib/CodeGen/CodeGenModule.cpp | 8 +++++++- clang/test/CodeGen/attributes.c | 3 +++ llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 5 +++++ llvm/test/CodeGen/X86/elf-unique-sections-by-flags.ll | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 173437e231b3..e840ce85c264 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2266,8 +2266,14 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, if (const auto *CSA = D->getAttr()) GO->setSection(CSA->getName()); - else if (const auto *SA = D->getAttr()) + // OHOS_LOCAL Begin + else if (const auto *SA = D->getAttr()) { GO->setSection(SA->getName()); + if ("_hilog_" == SA->getName()) + cast(GO)->setUnnamedAddr( + llvm::GlobalValue::UnnamedAddr::Global); + } + // OHOS_LOCAL End } getTargetCodeGenInfo().setTargetAttributes(D, GO, *this); diff --git a/clang/test/CodeGen/attributes.c b/clang/test/CodeGen/attributes.c index 95c6e67f1fea..8f4512c5298f 100644 --- a/clang/test/CodeGen/attributes.c +++ b/clang/test/CodeGen/attributes.c @@ -3,6 +3,9 @@ // CHECK: @t5 = weak{{.*}} global i32 2 int t5 __attribute__((weak)) = 2; +// CHECK: unnamed_addr +__attribute__((section("_hilog_"))) const char hilog_string1[] = "Hello"; + // CHECK: @t13 ={{.*}} global %struct.s0 zeroinitializer, section "SECT" struct s0 { int x; }; struct s0 t13 __attribute__((section("SECT"))) = { 0 }; diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 407ad034c18e..8119506d43b8 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -450,6 +450,11 @@ static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { // // .section .eh_frame,"a",@progbits + // OHOS_LOCAL Begin + if (Name == "_hilog_") + return SectionKind::getMergeable1ByteCString(); + // OHOS_LOCAL End + if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF, /*AddSegmentInfo=*/false) || Name == getInstrProfSectionName(IPSK_covfun, Triple::ELF, diff --git a/llvm/test/CodeGen/X86/elf-unique-sections-by-flags.ll b/llvm/test/CodeGen/X86/elf-unique-sections-by-flags.ll index 0ca08d4b3fea..142f2316c22f 100644 --- a/llvm/test/CodeGen/X86/elf-unique-sections-by-flags.ll +++ b/llvm/test/CodeGen/X86/elf-unique-sections-by-flags.ll @@ -53,6 +53,10 @@ define i32 @fn2_s2() section "s2" { ; CHECK: .globl fn2_s2 ; CHECK: fn2_s2: +; Check if the "_hilog_" section is set as mergeable +@hilog_var = global [14 x i8] c"Hello world!\0A\00", section "_hilog_" +; CHECK: .section _hilog_,"aMS",@progbits,1,unique,1 + ; Values that share a section name with a function are placed in different sections without executable flag @rw_s1 = global i32 10, section "s1", align 4 @ro_s2 = constant i32 10, section "s2", align 4 -- Gitee