Skip to content

Commit

Permalink
[CodeGen] Stop creating fake FunctionDecls when generating IR for
Browse files Browse the repository at this point in the history
functions implicitly generated by the compiler

These fake functions would cause clang to crash if the changes proposed
in https://reviews.llvm.org/D98799 were made.
  • Loading branch information
ahatanaka committed Jun 29, 2021
1 parent ac2bec5 commit 8d21d54
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 39 deletions.
10 changes: 1 addition & 9 deletions clang/lib/CodeGen/CGBuiltin.cpp
Expand Up @@ -1681,7 +1681,6 @@ llvm::Function *CodeGenFunction::generateBuiltinOSLogHelperFunction(
}

QualType ReturnTy = Ctx.VoidTy;
QualType FuncionTy = Ctx.getFunctionType(ReturnTy, ArgTys, {});

// The helper function has linkonce_odr linkage to enable the linker to merge
// identical functions. To ensure the merging always happens, 'noinline' is
Expand All @@ -1701,14 +1700,7 @@ llvm::Function *CodeGenFunction::generateBuiltinOSLogHelperFunction(
Fn->addFnAttr(llvm::Attribute::NoInline);

auto NL = ApplyDebugLocation::CreateEmpty(*this);
IdentifierInfo *II = &Ctx.Idents.get(Name);
FunctionDecl *FD = FunctionDecl::Create(
Ctx, Ctx.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II,
FuncionTy, nullptr, SC_PrivateExtern, false, false);
// Avoid generating debug location info for the function.
FD->setImplicit();

StartFunction(FD, ReturnTy, Fn, FI, Args);
StartFunction(GlobalDecl(), ReturnTy, Fn, FI, Args);

// Create a scope with an artificial location for the body of this function.
auto AL = ApplyDebugLocation::CreateArtificial(*this);
Expand Down
8 changes: 2 additions & 6 deletions clang/lib/CodeGen/CGNonTrivialStruct.cpp
Expand Up @@ -472,14 +472,10 @@ template <class Derived> struct GenFuncBase {
F->setVisibility(llvm::GlobalValue::HiddenVisibility);
CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F, /*IsThunk=*/false);
CGM.SetLLVMFunctionAttributesForDefinition(nullptr, F);
IdentifierInfo *II = &Ctx.Idents.get(FuncName);
FunctionDecl *FD = FunctionDecl::Create(
Ctx, Ctx.getTranslationUnitDecl(), SourceLocation(), SourceLocation(),
II, Ctx.getFunctionType(Ctx.VoidTy, llvm::None, {}), nullptr,
SC_PrivateExtern, false, false);
CodeGenFunction NewCGF(CGM);
setCGF(&NewCGF);
CGF->StartFunction(FD, Ctx.VoidTy, F, FI, Args);
CGF->StartFunction(GlobalDecl(), Ctx.VoidTy, F, FI, Args);
auto AL = ApplyDebugLocation::CreateArtificial(*CGF);
std::array<Address, N> Addrs =
getParamAddrs<N>(std::make_index_sequence<N>{}, Alignments, Args, CGF);
asDerived().visitStructFields(QT, CharUnits::Zero(), Addrs);
Expand Down
28 changes: 8 additions & 20 deletions clang/lib/CodeGen/ItaniumCXXABI.cpp
Expand Up @@ -2611,15 +2611,6 @@ static llvm::Function *createGlobalInitOrCleanupFn(CodeGen::CodeGenModule &CGM,
return GlobalInitOrCleanupFn;
}

static FunctionDecl *
createGlobalInitOrCleanupFnDecl(CodeGen::CodeGenModule &CGM, StringRef FnName) {
ASTContext &Ctx = CGM.getContext();
QualType FunctionTy = Ctx.getFunctionType(Ctx.VoidTy, llvm::None, {});
return FunctionDecl::Create(
Ctx, Ctx.getTranslationUnitDecl(), SourceLocation(), SourceLocation(),
&Ctx.Idents.get(FnName), FunctionTy, nullptr, SC_Static, false, false);
}

void CodeGenModule::unregisterGlobalDtorsWithUnAtExit() {
for (const auto &I : DtorsUsingAtExit) {
int Priority = I.first;
Expand All @@ -2629,13 +2620,11 @@ void CodeGenModule::unregisterGlobalDtorsWithUnAtExit() {
llvm::Function *GlobalCleanupFn =
createGlobalInitOrCleanupFn(*this, GlobalCleanupFnName);

FunctionDecl *GlobalCleanupFD =
createGlobalInitOrCleanupFnDecl(*this, GlobalCleanupFnName);

CodeGenFunction CGF(*this);
CGF.StartFunction(GlobalDecl(GlobalCleanupFD), getContext().VoidTy,
GlobalCleanupFn, getTypes().arrangeNullaryFunction(),
FunctionArgList(), SourceLocation(), SourceLocation());
CGF.StartFunction(GlobalDecl(), getContext().VoidTy, GlobalCleanupFn,
getTypes().arrangeNullaryFunction(), FunctionArgList(),
SourceLocation(), SourceLocation());
auto AL = ApplyDebugLocation::CreateArtificial(CGF);

// Get the destructor function type, void(*)(void).
llvm::FunctionType *dtorFuncTy = llvm::FunctionType::get(CGF.VoidTy, false);
Expand Down Expand Up @@ -2688,13 +2677,12 @@ void CodeGenModule::registerGlobalDtorsWithAtExit() {
std::string("__GLOBAL_init_") + llvm::to_string(Priority);
llvm::Function *GlobalInitFn =
createGlobalInitOrCleanupFn(*this, GlobalInitFnName);
FunctionDecl *GlobalInitFD =
createGlobalInitOrCleanupFnDecl(*this, GlobalInitFnName);

CodeGenFunction CGF(*this);
CGF.StartFunction(GlobalDecl(GlobalInitFD), getContext().VoidTy,
GlobalInitFn, getTypes().arrangeNullaryFunction(),
FunctionArgList(), SourceLocation(), SourceLocation());
CGF.StartFunction(GlobalDecl(), getContext().VoidTy, GlobalInitFn,
getTypes().arrangeNullaryFunction(), FunctionArgList(),
SourceLocation(), SourceLocation());
auto AL = ApplyDebugLocation::CreateArtificial(CGF);

// Since constructor functions are run in non-descending order of their
// priorities, destructors are registered in non-descending order of their
Expand Down
10 changes: 7 additions & 3 deletions clang/test/CodeGen/constructor-attribute.c
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=WITHOUTATEXIT %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -fregister-global-dtors-with-atexit -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=CXAATEXIT --check-prefix=WITHATEXIT %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -fregister-global-dtors-with-atexit -debug-info-kind=line-tables-only -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=CXAATEXIT --check-prefix=WITHATEXIT %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -fno-use-cxa-atexit -fregister-global-dtors-with-atexit -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=ATEXIT --check-prefix=WITHATEXIT %s

// WITHOUTATEXIT: global_ctors{{.*}}@A{{.*}}@C
Expand All @@ -19,8 +19,9 @@
// CHECK: define internal i32 @foo()
// WITHOUTATEXIT-NOT: define

// WITHATEXIT: define internal void @__GLOBAL_init_123(){{.*}}section "__TEXT,__StaticInit,regular,pure_instructions"
// CXAATEXIT: call i32 @__cxa_atexit(void (i8*)* bitcast (void ()* @E to void (i8*)*), i8* null, i8* @__dso_handle)
// CXAATEXIT: define internal void @__GLOBAL_init_123(){{.*}}section "__TEXT,__StaticInit,regular,pure_instructions" !dbg ![[GLOBAL_INIT_SP:.*]] {
// ATEXIT: define internal void @__GLOBAL_init_123(){{.*}}section "__TEXT,__StaticInit,regular,pure_instructions"
// CXAATEXIT: call i32 @__cxa_atexit(void (i8*)* bitcast (void ()* @E to void (i8*)*), i8* null, i8* @__dso_handle) {{.*}}, !dbg ![[GLOBAL_INIT_LOC:.*]]
// CXAATEXIT: call i32 @__cxa_atexit(void (i8*)* bitcast (void ()* @G to void (i8*)*), i8* null, i8* @__dso_handle)
// ATEXIT: call i32 @atexit(void ()* @E)
// ATEXIT: call i32 @atexit(void ()* @G)
Expand Down Expand Up @@ -82,3 +83,6 @@ static void D() {
int main() {
return 0;
}

// CXAATEXIT: ![[GLOBAL_INIT_SP]] = distinct !DISubprogram(linkageName: "__GLOBAL_init_123",
// CXAATEXIT: ![[GLOBAL_INIT_LOC]] = !DILocation(line: 0, scope: ![[GLOBAL_INIT_SP]])
2 changes: 1 addition & 1 deletion clang/test/CodeGen/debug-info-oslog.c
Expand Up @@ -10,6 +10,6 @@ void test_builtin_os_log(void *buf, int i, const char *data) {
// This helper is going to be uniqued, so it should not have a line
// number between file and type.

// CHECK: distinct !DISubprogram(name: "__os_log_helper_1_0_1_4_0",
// CHECK: distinct !DISubprogram(linkageName: "__os_log_helper_1_0_1_4_0",
// CHECK-SAME: file: !{{.*}}, type
// CHECK-SAME: flags: DIFlagArtificial
5 changes: 5 additions & 0 deletions clang/test/CodeGenObjC/nontrivial-c-struct-exception.m
Expand Up @@ -54,6 +54,9 @@ void testStrongException(void) {

// CHECK: resume

// CHECK: define{{.*}} void @__destructor_8_w8({{.*}} !dbg ![[DTOR_SP:.*]] {
// CHECK: load i8**, i8*** {{.*}}, !dbg ![[DTOR_LOC:.*]]

Weak genWeak(void);
void calleeWeak(Weak, Weak);

Expand All @@ -63,3 +66,5 @@ void testWeakException(void) {

// CHECK-DAG: [[ARTIFICIAL_LOC_1]] = !DILocation(line: 0
// CHECK-DAG: [[ARTIFICIAL_LOC_2]] = !DILocation(line: 0
// CHECK: ![[DTOR_SP]] = distinct !DISubprogram(linkageName: "__destructor_8_w8",
// CHECK: ![[DTOR_LOC]] = !DILocation(line: 0, scope: ![[DTOR_SP]])

0 comments on commit 8d21d54

Please sign in to comment.