[clang] [CIR] Implement NYI: forcecd externally visible def (PR #194663)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 28 09:04:05 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Zaky Hermawan (ZakyHermawan)

<details>
<summary>Changes</summary>

```c
inline void my_func() {}
extern inline void my_func();
```

compiling file above with `clang -cc1 -fclangir -emit-cir -std=c99 filename.c -o -`
will trigger NYI error, which this PR fixes it

Note: The stuff that I add in setFunctionAttributes is to handle error when calling getFunctionLinkage inside setFunctionAttributes.

Trace:

```
Assertion failed: ((doesThisDeclarationHaveABody() || willHaveBody() || hasAttr<AliasAttr>()) && "Must be a function definition"), function isInlineDefinitionExternallyVisible, file Decl.cpp, line 4047.

PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.

Stack dump:

0. Program arguments: clang -cc1 -fclangir -emit-cir -std=c99 opt.c -o -

1. <eof> parser at end of file

 #<!-- -->0 0x000000010881eca8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x1019ceca8)

 #<!-- -->1 0x000000010881bf2f llvm::sys::RunSignalHandlers() (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x1019cbf2f)

 #<!-- -->2 0x000000010881f865 SignalHandler(int, __siginfo*, void*) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x1019cf865)

 #<!-- -->3 0x000000010881f6fe SignalHandlerTerminate(int, __siginfo*, void*) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x1019cf6fe)

 #<!-- -->4 0x00007ff80807a31d (/usr/lib/system/libsystem_platform.dylib+0x7ff801f0c31d)

 #<!-- -->5 0x00007ff7b90a67f8

 #<!-- -->6 0x00007ff807f605e6 (/usr/lib/system/libsystem_c.dylib+0x7ff801df25e6)

 #<!-- -->7 0x00007ff807f5f808 (/usr/lib/system/libsystem_c.dylib+0x7ff801df1808)

 #<!-- -->8 0x000000010eb64563 clang::FunctionDecl::isInlineDefinitionExternallyVisible() const (.cold.2) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x107d14563)

 #<!-- -->9 0x000000010eb54ea3 clang::FunctionDecl::isInlineDefinitionExternallyVisible() const (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x107d04ea3)

#<!-- -->10 0x000000010e8c4890 basicGVALinkageForFunction(clang::ASTContext const&, clang::FunctionDecl const*) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x107a74890)

#<!-- -->11 0x000000010e8c4455 clang::ASTContext::GetGVALinkageForFunction(clang::FunctionDecl const*) const (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x107a74455)

#<!-- -->12 0x0000000109fa45f7 clang::CIRGen::CIRGenModule::getFunctionLinkage(clang::GlobalDecl) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x1031545f7)

#<!-- -->13 0x0000000109fa6d64 clang::CIRGen::CIRGenModule::setFunctionAttributes(clang::GlobalDecl, cir::FuncOp, bool, bool) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x103156d64)

#<!-- -->14 0x0000000109f9ced9 clang::CIRGen::CIRGenModule::getOrCreateCIRFunction(llvm::StringRef, mlir::Type, clang::GlobalDecl, bool, bool, bool, clang::CIRGen::ForDefinition_t, mlir::NamedAttrList) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x10314ced9)

#<!-- -->15 0x0000000109f9c6fa clang::CIRGen::CIRGenModule::emitGlobal(clang::GlobalDecl) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x10314c6fa)

#<!-- -->16 0x0000000109f9eb50 clang::CIRGen::CIRGenModule::emitTopLevelDecl(clang::Decl*) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x10314eb50)

#<!-- -->17 0x0000000109e5145c cir::CIRGenerator::HandleTopLevelDecl(clang::DeclGroupRef) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x10300145c)

#<!-- -->18 0x0000000109cf8b8e cir::CIRGenConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x102ea8b8e)

#<!-- -->19 0x000000010d759529 clang::ParseAST(clang::Sema&, bool, bool) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x106909529)

#<!-- -->20 0x00000001098a4b4a clang::FrontendAction::Execute() (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x102a54b4a)

#<!-- -->21 0x00000001097f2e7a clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x1029a2e7a)

#<!-- -->22 0x00000001099d8938 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x102b88938)

#<!-- -->23 0x0000000106e573ff cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x1000073ff)

#<!-- -->24 0x0000000106e53d12 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x100003d12)

#<!-- -->25 0x0000000106e52f22 clang_main(int, char**, llvm::ToolContext const&) (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x100002f22)

#<!-- -->26 0x0000000106e62956 main (/Users/macbook/Workspace/llvm-project/build/bin/clang-23+0x100012956)

#<!-- -->27 0x00007ff807c9c530

zsh: abort      clang -cc1 -fclangir -emit-cir -std=c99 opt.c -o -


```


---
Full diff: https://github.com/llvm/llvm-project/pull/194663.diff


2 Files Affected:

- (modified) clang/lib/CIR/CodeGen/CIRGenModule.cpp (+18-2) 
- (added) clang/test/CIR/CodeGen/force-externally-visibile-def.c (+12) 


``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 7d719600a6025..230f20971f7c5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -517,12 +517,20 @@ void CIRGenModule::emitGlobal(clang::GlobalDecl gd) {
       if (getGlobalValue(mangledName))
         deferredAnnotations[mangledName] = fd;
     }
+
+    // Forward declarations are emitted lazily on first use.
     if (!fd->doesThisDeclarationHaveABody()) {
       if (!fd->doesDeclarationForceExternallyVisibleDefinition())
         return;
 
-      errorNYI(fd->getSourceRange(),
-               "function declaration that forces code gen");
+      StringRef mangledName = getMangledName(gd);
+
+      // Compute the function info and CIR func type.
+      const CIRGenFunctionInfo &fi = getTypes().arrangeGlobalDeclaration(gd);
+      cir::FuncType ty = getTypes().getFunctionType(fi);
+
+      getOrCreateCIRFunction(mangledName, ty, gd, /*ForVTable=*/false,
+                              /*DontDefer=*/false);
       return;
     }
   } else {
@@ -2716,7 +2724,15 @@ void CIRGenModule::setFunctionAttributes(GlobalDecl globalDecl,
   // represent them in dedicated ops. The correct attributes are ensured during
   // translation to LLVM. Thus, we don't need to check for them here.
 
+  // If this function has a body somewhere in the AST, we must use that
+  // definition for attributes and linkage calculation. This prevents AST 
+  // assertions when forcing inline definitions from forward declarations.
   const auto *funcDecl = cast<FunctionDecl>(globalDecl.getDecl());
+  const FunctionDecl *fdDef;
+  if (funcDecl->hasBody(fdDef)) {
+    funcDecl = fdDef;
+    globalDecl = globalDecl.getWithDecl(fdDef);
+  }
 
   if (!isIncompleteFunction)
     setCIRFunctionAttributes(globalDecl,
diff --git a/clang/test/CIR/CodeGen/force-externally-visibile-def.c b/clang/test/CIR/CodeGen/force-externally-visibile-def.c
new file mode 100644
index 0000000000000..133e49211b8ff
--- /dev/null
+++ b/clang/test/CIR/CodeGen/force-externally-visibile-def.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fclangir -emit-cir -std=c99 %s -o - | FileCheck %s
+
+inline void my_func() {}
+
+// Force the externally visible definition
+extern inline void my_func();
+
+// CHECK: module {{.*}} attributes {cir.lang = #cir.lang<c>{{.*}} {
+// CHECK-NEXT:   cir.func no_inline no_proto @my_func() attributes {nothrow} {
+// CHECK-NEXT:     cir.return
+// CHECK-NEXT:   }
+// CHECK-NEXT: }

``````````

</details>


https://github.com/llvm/llvm-project/pull/194663


More information about the cfe-commits mailing list