https://github.com/ChuanqiXu9 updated https://github.com/llvm/llvm-project/pull/93530
>From eaf720ca9d3a6a576eefbf9ac553b108611ec00d Mon Sep 17 00:00:00 2001 From: Chuanqi Xu <yedeng...@linux.alibaba.com> Date: Wed, 29 May 2024 10:48:51 +0800 Subject: [PATCH] [C++20] [Modules] Don't generate the defintition for non-const available external variables Close #93497 The root cause of the problem is, we mark the variable from other modules as constnant in LLVM incorrectly. This patch fixes this problem by not emitting the defintition for non-const available external variables. Since the non const available externally variable is not helpful to the optimization. --- clang/lib/CodeGen/CodeGenModule.cpp | 9 +++ clang/test/CodeGenCXX/partitions.cpp | 8 +-- clang/test/Modules/pr93497.cppm | 83 ++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 clang/test/Modules/pr93497.cppm diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index e4774a587707a..77ce49ea46120 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -5341,6 +5341,15 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, !IsDefinitionAvailableExternally && D->needsDestruction(getContext()) == QualType::DK_cxx_destructor; + // It is helpless to emit the definition for an available_externally variable + // which can't be marked as const. + // We don't need to check if it needs global ctor or dtor. See the above comment + // for ideas. + if (IsDefinitionAvailableExternally && + (!D->hasConstantInitialization() || + !D->getType().isConstantStorage(getContext(), true, true))) + return; + const VarDecl *InitDecl; const Expr *InitExpr = D->getAnyInitializer(InitDecl); diff --git a/clang/test/CodeGenCXX/partitions.cpp b/clang/test/CodeGenCXX/partitions.cpp index d283dd071f6b2..e80e68f82974b 100644 --- a/clang/test/CodeGenCXX/partitions.cpp +++ b/clang/test/CodeGenCXX/partitions.cpp @@ -40,12 +40,12 @@ export int use() { } // FIXME: The definition of the variables shouldn't be exported too. -// CHECK: @_ZW3mod1a = available_externally global -// CHECK: @_ZW3mod1b = available_externally global +// CHECK: @_ZW3mod1a = external global +// CHECK: @_ZW3mod1b = external global // CHECK: declare{{.*}} i32 @_ZW3mod3foov // CHECK: declare{{.*}} i32 @_ZW3mod3barv -// CHECK-OPT: @_ZW3mod1a = available_externally global -// CHECK-OPT: @_ZW3mod1b = available_externally global +// CHECK-OPT: @_ZW3mod1a = external global +// CHECK-OPT: @_ZW3mod1b = external global // CHECK-OPT: declare{{.*}} i32 @_ZW3mod3foov // CHECK-OPT: declare{{.*}} i32 @_ZW3mod3barv diff --git a/clang/test/Modules/pr93497.cppm b/clang/test/Modules/pr93497.cppm new file mode 100644 index 0000000000000..fa88eadabdcd7 --- /dev/null +++ b/clang/test/Modules/pr93497.cppm @@ -0,0 +1,83 @@ +// RUN: rm -rf %t +// RUN: mkdir -p %t +// RUN: split-file %s %t + +// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/mod.cppm \ +// RUN: -emit-module-interface -o %t/mod.pcm +// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/use.cpp \ +// RUN: -fmodule-file=mod=%t/mod.pcm -emit-llvm \ +// RUN: -o - | FileCheck %t/use.cpp + +//--- mod.cppm +export module mod; + +export struct Thing { + static const Thing One; + explicit Thing(int raw) :raw(raw) { } + int raw; +}; + +const Thing Thing::One = Thing(1); + +export struct C { + int value; +}; +export const C ConstantValue = {1}; + +export const C *ConstantPtr = &ConstantValue; + +C NonConstantValue = {1}; +export const C &ConstantRef = NonConstantValue; + +//--- use.cpp +import mod; + +int consume(int); +int consumeC(C); + +extern "C" __attribute__((noinline)) inline int unneeded() { + return consume(43); +} + +extern "C" __attribute__((noinline)) inline int needed() { + return consume(43); +} + +int use() { + Thing t1 = Thing::One; + return consume(t1.raw); +} + +int use2() { + if (ConstantValue.value) + return consumeC(ConstantValue); + return unneeded(); +} + +int use3() { + if (ConstantPtr->value) + return consumeC(*ConstantPtr); + return needed(); +} + +int use4() { + if (ConstantRef.value) + return consumeC(ConstantRef); + return needed(); +} + +// CHECK: @_ZNW3mod5Thing3OneE = external +// CHECK: @_ZW3mod13ConstantValue ={{.*}}available_externally{{.*}} constant +// CHECK: @_ZW3mod11ConstantPtr = external +// CHECK: @_ZW3mod16NonConstantValue = external + +// Check that the middle end can optimize the program by the constant information. +// CHECK-NOT: @unneeded( + +// Check that the use of ConstantPtr won't get optimized incorrectly. +// CHECK-LABEL: @_Z4use3v( +// CHECK: @needed( + +// Check that the use of ConstantRef won't get optimized incorrectly. +// CHECK-LABEL: @_Z4use4v( +// CHECK: @needed( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits