Skip to contentSkip to conversation

kbuild: add support to generate LLVM bitcode files

PATCH Superseded Vinícius Tinti 2 participants 9 messages Jul 19, 2014 – Sep 11, 2014 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: add support to generate LLVM bitcode files

Allows kbuild to generate LLVM bitcode files with the .ll extension when building with Clang.

  # c code
  CC=clang make kernel/pid.ll
  # asm code
  CC=clang make arch/arm/kernel/calls.ll
---
 Makefile               |  7 +++++++
 scripts/Makefile.build | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+)
Makefile +7 -0
index f3c543d..4eb0d14 100644
@@ -1484,6 +1484,13 @@ endif
 %.symtypes: %.c prepare scripts FORCE
 	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 
+ifeq ($(COMPILER),clang)
+%.ll: %.c prepare scripts FORCE
+	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+%.ll: %.S prepare scripts FORCE
+	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+endif
+
 # Modules
 /: prepare scripts FORCE
 	$(cmd_crmodverdir)
scripts/Makefile.build +18 -0
index bf3e677..9ea19d6 100644
@@ -174,6 +174,14 @@ cmd_cc_symtypes_c =                                                         \
 $(obj)/%.symtypes : $(src)/%.c FORCE
 	$(call cmd,cc_symtypes_c)
 
+ifeq ($(COMPILER),clang)
+quiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
+cmd_cc_ll_c       = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -emit-llvm -S -o $@ $<
+
+$(obj)/%.ll: $(src)/%.c FORCE
+	$(call if_changed_dep,cc_ll_c)
+endif
+
 # C (.c) files
 # The C file is compiled and updated dependency information is generated.
 # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
@@ -315,6 +323,16 @@ quiet_cmd_asn1_compiler = ASN.1   $@
 $(obj)/%-asn1.c $(obj)/%-asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
 	$(call cmd,asn1_compiler)
 
+# LLVM bitcode
+# ---------------------------------------------------------------------------
+ifeq ($(COMPILER),clang)
+quiet_cmd_as_ll_S = CPP $(quiet_modtag) $@
+cmd_as_ll_S       = $(CPP) $(a_flags)   -o $@ $<
+
+$(obj)/%.ll: $(src)/%.S FORCE
+	$(call if_changed_dep,as_ll_S)
+endif
+
 # Build the compiled-in targets
 # ---------------------------------------------------------------------------
 
Signature · 1 line
2.0.1
#1 lore raw
Quoted 21 lines · Vinícius Tinti

Allows kbuild to generate LLVM bitcode files with the .ll extension when building with Clang.

  # c code
  CC=clang make kernel/pid.ll
  # asm code
  CC=clang make arch/arm/kernel/calls.ll
---
 Makefile               |  7 +++++++
 scripts/Makefile.build | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+)
Makefile +0 -0
index f3c543d..4eb0d14 100644

Makefile endif · line 1492

Quoted 9 lines
Makefile +6 -0
@@ -1484,6 +1484,13 @@ endif
 %.symtypes: %.c prepare scripts FORCE
 	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 
+ifeq ($(COMPILER),clang)
+%.ll: %.c prepare scripts FORCE
+	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+%.ll: %.S prepare scripts FORCE
+	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+endif

Can we drop the test for $(COMPILER) here?
If one try this with gcc then gcc will just fail if it do not support the clang flags supplied.

And help section should be updated to list .ll too.

scripts/Makefile.build cmd_cc_symtypes_c = \ · line 179

Quoted 11 lines
scripts/Makefile.build +3 -0
index bf3e677..9ea19d6 100644
@@ -174,6 +174,14 @@ cmd_cc_symtypes_c =                                                         \
 $(obj)/%.symtypes : $(src)/%.c FORCE
 	$(call cmd,cc_symtypes_c)
 
+ifeq ($(COMPILER),clang)
+quiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
+cmd_cc_ll_c       = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -emit-llvm -S -o $@ $<

Can we drop the test for COMPILER here too? Do -fverbose-asm make sense when generating .ll files? It looks like a leftover from what you copied.

scripts/Makefile.build quiet_cmd_asn1_compiler = ASN.1 $@ · line 334

Quoted 9 lines
scripts/Makefile.build +9 -0
+# LLVM bitcode
+# ---------------------------------------------------------------------------
+ifeq ($(COMPILER),clang)
+quiet_cmd_as_ll_S = CPP $(quiet_modtag) $@
+cmd_as_ll_S       = $(CPP) $(a_flags)   -o $@ $<
+
+$(obj)/%.ll: $(src)/%.S FORCE
+	$(call if_changed_dep,as_ll_S)
+endif

This chunk belongs together with the other chunk. There is no reason to separate .S => .ll and .c => .ll rules.

The current rules for .c => .lst etc is a mess and not something to be too much inspired from.

Sam

#2 lore raw
Quoted 8 lines

scripts/Makefile.build cmd_cc_symtypes_c = \ · line 179

Quoted 4 lines
scripts/Makefile.build +3 -0
+ifeq ($(COMPILER),clang)
+quiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
+cmd_cc_ll_c       = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -emit-llvm -S -o $@ $<

Can we drop the test for COMPILER here too? Do -fverbose-asm make sense when generating .ll files? It looks like a leftover from what you copied.

Also $(DISABLE_LTO) looks like a left-over.
It was added to avoid LTO when generating asm-offstes.s which the original rule is also used for.
So this should be skipped too.

Sam

#3 lore raw
Quoted 9 lines · Sam Ravnborg
Quoted 8 lines

scripts/Makefile.build cmd_cc_symtypes_c = \ · line 179

Quoted 4 lines
scripts/Makefile.build +3 -0
+ifeq ($(COMPILER),clang)
+quiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
+cmd_cc_ll_c       = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -emit-llvm -S -o $@ $<

Can we drop the test for COMPILER here too? Do -fverbose-asm make sense when generating .ll files? It looks like a leftover from what you copied.

Quoted 4 lines

Also $(DISABLE_LTO) looks like a left-over.
It was added to avoid LTO when generating asm-offstes.s which the original rule is also used for.
So this should be skipped too.

Third thing.
In some places we indent the assignmnet like this:
quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@

      cmd_cc_ll_c = $(CC) $(c_flags) ....

This makes it more obvious that the "cmd_cc_ll_c" is actually the same. Makefile.build does not do this much but please do for these targets.

Sam

author
#4 lore raw

1) Can we drop the test for $(COMPILER) here?

Yes.

2) And help section should be updated to list .ll too.

I will be adding it too.

3) This chunk belongs together with the other chunk. There is no reason to separate .S => .ll and .c => .ll rules.

Sure. But in fact I was not able to create the .ll from .S and neither create the .s
from the .S in some assembly files on kernel. Do you know how it works?

4) Also $(DISABLE_LTO) looks like a left-over.

Remove $(DISABLE_LTO)

5) quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@

      cmd_cc_ll_c = $(CC) $(c_flags) ....

Understood.

Thanks.

Quoted 23 lines · Sam Ravnborg
Quoted 9 lines · Sam Ravnborg
Quoted 8 lines

scripts/Makefile.build cmd_cc_symtypes_c = \ · line 179

Quoted 4 lines
scripts/Makefile.build +3 -0
+ifeq ($(COMPILER),clang)
+quiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
+cmd_cc_ll_c       = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -emit-llvm -S -o $@ $<

Can we drop the test for COMPILER here too? Do -fverbose-asm make sense when generating .ll files? It looks like a leftover from what you copied.

Quoted 4 lines

Also $(DISABLE_LTO) looks like a left-over.
It was added to avoid LTO when generating asm-offstes.s which the original rule is also used for.
So this should be skipped too.

Third thing.
In some places we indent the assignmnet like this:
quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@

      cmd_cc_ll_c = $(CC) $(c_flags) ....

This makes it more obvious that the "cmd_cc_ll_c" is actually the same. Makefile.build does not do this much but please do for these targets.

Sam

Signature · 1 line
Simplicity is the ultimate sophistication
#5 lore raw
Quoted 4 lines

Sure. But in fact I was not able to create the .ll from .S and neither create the .s
from the .S in some assembly files on kernel. Do you know how it works?

make arch/x86/kernel/preempt.s

Works for me.
Something broke in your setup?

Sam

#6 lore raw

PATCH v2 1/1 kbuild: add support to generate LLVM bitcode files

In reply to Tinti

Allows kbuild to generate LLVM bitcode files with the .ll extension.

  # from c code
  CC=clang make kernel/pid.ll
  # from asm code
  CC=clang make arch/x86/kernel/preempt.ll
---
 .gitignore             |  1 +
 Makefile               |  6 ++++++
 scripts/Makefile.build | 14 ++++++++++++++
 3 files changed, 21 insertions(+)
.gitignore +1 -0
index f4c0b09..c5a7656 100644
@@ -32,6 +32,7 @@
 *.lzo
 *.patch
 *.gcno
+*.ll
 modules.builtin
 Module.symvers
 
Makefile +6 -0
index 6b27741..7826c33 100644
@@ -1213,6 +1213,8 @@ help:
 	@echo  '                    (default: $$(INSTALL_MOD_PATH)/lib/firmware)'
 	@echo  '  dir/            - Build all files in dir and below'
 	@echo  '  dir/file.[oisS] - Build specified target only'
+	@echo  '  dir/file.ll     - Build the LLVM bitcode file'
+	@echo  '                    (requires a compiler support for LLVM bitcode generation)'
 	@echo  '  dir/file.lst    - Build specified mixed source/assembly target only'
 	@echo  '                    (requires a recent binutils and recent build (System.map))'
 	@echo  '  dir/file.ko     - Build module including final link'
@@ -1483,6 +1485,10 @@ endif
 	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 %.symtypes: %.c prepare scripts FORCE
 	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+%.ll: %.c prepare scripts FORCE
+	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+%.ll: %.S prepare scripts FORCE
+	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 
 # Modules
 /: prepare scripts FORCE
scripts/Makefile.build +14 -0
index bf3e677..4d97e4f 100644
@@ -174,6 +174,20 @@ cmd_cc_symtypes_c =                                                         \
 $(obj)/%.symtypes : $(src)/%.c FORCE
 	$(call cmd,cc_symtypes_c)
 
+# LLVM bitcode
+# Generate .ll files from .s and .c
+quiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
+      cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -o $@ $<
+
+$(obj)/%.ll: $(src)/%.c FORCE
+	$(call if_changed_dep,cc_ll_c)
+
+quiet_cmd_as_ll_S = CPP $(quiet_modtag) $@
+      cmd_as_ll_S = $(CPP) $(a_flags)   -o $@ $<
+
+$(obj)/%.ll: $(src)/%.S FORCE
+	$(call if_changed_dep,as_ll_S)
+
 # C (.c) files
 # The C file is compiled and updated dependency information is generated.
 # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
Signature · 1 line
2.0.1
author
#7 lore raw
Quoted 78 lines · Vinícius Tinti

Allows kbuild to generate LLVM bitcode files with the .ll extension.

  # from c code
  CC=clang make kernel/pid.ll
  # from asm code
  CC=clang make arch/x86/kernel/preempt.ll
---
 .gitignore             |  1 +
 Makefile               |  6 ++++++
 scripts/Makefile.build | 14 ++++++++++++++
 3 files changed, 21 insertions(+)
.gitignore +1 -0
index f4c0b09..c5a7656 100644
@@ -32,6 +32,7 @@
 *.lzo
 *.patch
 *.gcno
+*.ll
 modules.builtin
 Module.symvers
 
Makefile +6 -0
index 6b27741..7826c33 100644
@@ -1213,6 +1213,8 @@ help:
        @echo  '                    (default: $$(INSTALL_MOD_PATH)/lib/firmware)'
        @echo  '  dir/            - Build all files in dir and below'
        @echo  '  dir/file.[oisS] - Build specified target only'
+       @echo  '  dir/file.ll     - Build the LLVM bitcode file'
+       @echo  '                    (requires a compiler support for LLVM bitcode generation)'
        @echo  '  dir/file.lst    - Build specified mixed source/assembly target only'
        @echo  '                    (requires a recent binutils and recent build (System.map))'
        @echo  '  dir/file.ko     - Build module including final link'
@@ -1483,6 +1485,10 @@ endif
        $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 %.symtypes: %.c prepare scripts FORCE
        $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+%.ll: %.c prepare scripts FORCE
+       $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+%.ll: %.S prepare scripts FORCE
+       $(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 
 # Modules
 /: prepare scripts FORCE
scripts/Makefile.build +14 -1
index bf3e677..4d97e4f 100644
@@ -174,6 +174,20 @@ cmd_cc_symtypes_c =                                                         \
 $(obj)/%.symtypes : $(src)/%.c FORCE
        $(call cmd,cc_symtypes_c)
 
+# LLVM bitcode
+# Generate .ll files from .s and .c
+quiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
+      cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -o $@ $<
+
+$(obj)/%.ll: $(src)/%.c FORCE
+       $(call if_changed_dep,cc_ll_c)
+
+quiet_cmd_as_ll_S = CPP $(quiet_modtag) $@
+      cmd_as_ll_S = $(CPP) $(a_flags)   -o $@ $<
+
+$(obj)/%.ll: $(src)/%.S FORCE
+       $(call if_changed_dep,as_ll_S)
+
 # C (.c) files
 # The C file is compiled and updated dependency information is generated.
 # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
--
Quoted 2 lines

2.0.1

The preempt.s works for me as well the preempt.ll.

The make help now displays about the .ll files but I have not found other section in Documentation about it. If there is please let me know. I have also added the *.ll in .gitignore.

Regards

Signature · 1 line
Simplicity is the ultimate sophistication
#8 lore raw

PATCH v3 1/1 kbuild: add support to generate LLVM bitcode files

Allows kbuild to generate LLVM bitcode files using '.ll' suffix.

  # from c code
  CC=clang make kernel/pid.ll
  # from asm code
  CC=clang make arch/x86/kernel/preempt.ll
---
 .gitignore             |  1 +
 Makefile               |  6 ++++++
 scripts/Makefile.build | 14 ++++++++++++++
 3 files changed, 21 insertions(+)
.gitignore +1 -0
index e213b27..823b9d6 100644
@@ -32,6 +32,7 @@
 *.lzo
 *.patch
 *.gcno
+*.ll
 modules.builtin
 Module.symvers
 *.dwo
Makefile +6 -0
index 1a60bdd..cd4a120 100644
@@ -1250,6 +1250,8 @@ help:
 	@echo  '                    (default: $$(INSTALL_MOD_PATH)/lib/firmware)'
 	@echo  '  dir/            - Build all files in dir and below'
 	@echo  '  dir/file.[oisS] - Build specified target only'
+	@echo  '  dir/file.ll     - Build the LLVM bitcode file'
+	@echo  '                    (requires compiler support for LLVM bitcode generation)'
 	@echo  '  dir/file.lst    - Build specified mixed source/assembly target only'
 	@echo  '                    (requires a recent binutils and recent build (System.map))'
 	@echo  '  dir/file.ko     - Build module including final link'
@@ -1526,6 +1528,10 @@ endif
 	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 %.symtypes: %.c prepare scripts FORCE
 	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+%.ll: %.c prepare scripts FORCE
+	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+%.ll: %.S prepare scripts FORCE
+	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 
 # Modules
 /: prepare scripts FORCE
scripts/Makefile.build +14 -0
index bf3e677..4d97e4f 100644
@@ -174,6 +174,20 @@ cmd_cc_symtypes_c =                                                         \
 $(obj)/%.symtypes : $(src)/%.c FORCE
 	$(call cmd,cc_symtypes_c)
 
+# LLVM bitcode
+# Generate .ll files from .s and .c
+quiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
+      cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -o $@ $<
+
+$(obj)/%.ll: $(src)/%.c FORCE
+	$(call if_changed_dep,cc_ll_c)
+
+quiet_cmd_as_ll_S = CPP $(quiet_modtag) $@
+      cmd_as_ll_S = $(CPP) $(a_flags)   -o $@ $<
+
+$(obj)/%.ll: $(src)/%.S FORCE
+	$(call if_changed_dep,as_ll_S)
+
 # C (.c) files
 # The C file is compiled and updated dependency information is generated.
 # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
Signature · 1 line
2.1.0