From 9477f72252e8ad735966bf3ec7e892066077f452 Mon Sep 17 00:00:00 2001 From: zhengweiwei Date: Wed, 12 Feb 2025 10:01:21 +0800 Subject: [PATCH 1/2] Deduplicate strings in the hilog section --- clang/lib/CodeGen/CodeGenModule.cpp | 9 +++++++++ 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, 21 insertions(+) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 173437e231b3..b25f74e0b167 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2266,8 +2266,17 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, if (const auto *CSA = D->getAttr()) GO->setSection(CSA->getName()); + #ifdef __OHOS_FAMILY__ + else if (const auto *SA = D->getAttr()) { + GO->setSection(SA->getName()); + if ("_hilog1" == SA->getName()) + cast(GO)->setUnnamedAddr( + llvm::GlobalValue::UnnamedAddr::Global); + } + #else else if (const auto *SA = D->getAttr()) GO->setSection(SA->getName()); + #endif } 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..77c5f854e749 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 + #ifdef __OHOS_FAMILY__ + if (Name == "_hilog1") + return SectionKind::getMergeable1ByteCString(); + #endif + 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 From b27f18ba928d6c39c9d9662d5d40564309ee1020 Mon Sep 17 00:00:00 2001 From: zhengweiwei Date: Wed, 12 Feb 2025 11:58:29 +0800 Subject: [PATCH 2/2] 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 | 2 +- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index b25f74e0b167..8eb08b174d7b 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2269,7 +2269,7 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, #ifdef __OHOS_FAMILY__ else if (const auto *SA = D->getAttr()) { GO->setSection(SA->getName()); - if ("_hilog1" == SA->getName()) + if ("_hilog_" == SA->getName()) cast(GO)->setUnnamedAddr( llvm::GlobalValue::UnnamedAddr::Global); } diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 77c5f854e749..12ff73f68a32 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -451,7 +451,7 @@ static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { // .section .eh_frame,"a",@progbits #ifdef __OHOS_FAMILY__ - if (Name == "_hilog1") + if (Name == "_hilog_") return SectionKind::getMergeable1ByteCString(); #endif -- Gitee