Issue 97244
Summary [clang] module should not emit module initializer if it is not used in the module
Labels clang
Assignees
Reporter trcrsired
    ```cpp
//mymodule.cppm
module;

export module mymodule;
```
```cpp
//main.cc
import mymodule;

int main(){}
```
```
clang++ --precompile mymodule.cppm -std=c++26 -Ofast -o mymodule.gcm
clang++ -o main main.cc -std=c++26 -Ofast -fmodule-file=mymodule.gcm -fuse
-ld=lld
warning: the form '-fmodule-file=<BMI-path>' is deprecated for standard C++ named modules;consider to use
      '-fmodule-file=<module-name>=<BMI-path>' instead [-Weager-load-cxx-named-modules]
1 warning generated.
ld.lld: error: undefined symbol: initializer for module mymodule
>>> referenced by C:/Users/unlvs/AppData/Local/Temp/main-c847e4.o:(_GLOBAL__sub_I_main.cc)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```
The asm it generates will "jmp	_ZGIW8mymodule". However, GCC does not emit that after my complaint before. If the module has global variables, it should emit no symbol.
```
	.text
	.def	@feat.00;
	.scl	3;
	.type	0;
	.endef
	.globl	@feat.00
.set @feat.00, 0
	.file	"main.cc"
	.def	main;
	.scl	2;
	.type	32;
	.endef
	.globl	main # -- Begin function main
	.p2align	4, 0x90
main:                                   # @main
.seh_proc main
# %bb.0:
	pushq	%rbp
	.seh_pushreg %rbp
	subq	$32, %rsp
	.seh_stackalloc 32
	leaq	32(%rsp), %rbp
	.seh_setframe %rbp, 32
	.seh_endprologue
	callq	__main
	xorl	%eax, %eax
	addq	$32, %rsp
	popq	%rbp
	retq
	.seh_endproc
 # -- End function
	.def	_GLOBAL__sub_I_main.cc;
	.scl	3;
	.type	32;
	.endef
	.p2align	4, 0x90                         # -- Begin function _GLOBAL__sub_I_main.cc
_GLOBAL__sub_I_main.cc:                 # @_GLOBAL__sub_I_main.cc
# %bb.0:
	jmp	_ZGIW8mymodule # TAILCALL
                                        # -- End function
	.section	.ctors,"dw"
	.p2align	3, 0x0
	.quad	_GLOBAL__sub_I_main.cc
	.section	.drectve,"yni"
	.addrsig
	.addrsig_sym _GLOBAL__sub_I_main.cc
```

```
g++ -c mymodule.cppm -Ofast -std=c++26 -fmodules-ts
g++ -o main main.cc mymodule.o -Ofast -std=c++26 -fmodules-ts
```

```
g++ -c mymodule.cppm -Ofast -std=c++26 -fmodules-ts
g++ -S main.cc mymodule.o -Ofast -std=c++26 -fmodules-ts
g++: warning: mymodule.o: linker input file unused because linking not done

	.file	"main.cc"
	.text
	.section	.text.startup,"x"
	.p2align 4
	.globl	main
	.def	main;	.scl	2;	.type	32;	.endef
	.seh_proc	main
main:
.LFB0:
	subq	$40, %rsp
	.seh_stackalloc	40
	.seh_endprologue
	call	__main
	xorl	%eax, %eax
	addq	$40, %rsp
	ret
	.seh_endproc
	.def	__main;	.scl	2;	.type	32;	.endef
	.ident	"GCC: (GNU) 15.0.0 20240629 (experimental)"
```

Also, I do think gcc just reuses -c option and .o file which is a better choice plus clang should be compatible with GCC
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to