git: 2f49e40a684a - main - llvm: add LoongArch target support, not enabled by default

From: Dimitry Andric <dim_at_FreeBSD.org>
Date: Mon, 28 Sep 2026 16:13:22 UTC
The branch main has been updated by dim:

URL: https://cgit.FreeBSD.org/src/commit/?id=2f49e40a684a4bffd8e368a6878384ec4f735ea8

commit 2f49e40a684a4bffd8e368a6878384ec4f735ea8
Author:     Xiaoqiang Zhao <zhaoxiaoqiang007@gmail.com>
AuthorDate: 2026-09-28 15:14:09 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2026-09-28 16:13:02 +0000

    llvm: add LoongArch target support, not enabled by default
    
    Note there is ongoing work to add LoongArch support to the base system,
    but having target support in llvm is an essential component.
    
    This must be explicitly enabled using WITH_LLVM_TARGET_LOONGARCH.
    
    Reviewed by:    dim
    MFC after:      1 week
    Differential Revision: https://reviews.freebsd.org/D59899
---
 lib/clang/include/llvm/Config/AsmParsers.def      |  3 ++
 lib/clang/include/llvm/Config/AsmPrinters.def     |  3 ++
 lib/clang/include/llvm/Config/Disassemblers.def   |  3 ++
 lib/clang/include/llvm/Config/Targets.def         |  3 ++
 lib/clang/libllvm/Makefile                        | 55 ++++++++++++++++++++---
 lib/clang/llvm.build.mk                           |  6 +++
 share/man/man5/src.conf.5                         |  7 ++-
 share/mk/src.opts.mk                              |  2 +-
 tools/build/options/WITHOUT_LLVM_TARGET_BPF       |  1 +
 tools/build/options/WITHOUT_LLVM_TARGET_LOONGARCH |  1 +
 tools/build/options/WITH_LLVM_TARGET_BPF          |  3 --
 tools/build/options/WITH_LLVM_TARGET_LOONGARCH    |  1 +
 12 files changed, 74 insertions(+), 14 deletions(-)

diff --git a/lib/clang/include/llvm/Config/AsmParsers.def b/lib/clang/include/llvm/Config/AsmParsers.def
index 8cf3ffd6b0d4..a10e12bb10c7 100644
--- a/lib/clang/include/llvm/Config/AsmParsers.def
+++ b/lib/clang/include/llvm/Config/AsmParsers.def
@@ -33,6 +33,9 @@ LLVM_ASM_PARSER(ARM)
 #ifdef LLVM_TARGET_ENABLE_BPF
 LLVM_ASM_PARSER(BPF)
 #endif
+#ifdef LLVM_TARGET_ENABLE_LOONGARCH
+LLVM_ASM_PARSER(LoongArch)
+#endif
 #ifdef LLVM_TARGET_ENABLE_POWERPC
 LLVM_ASM_PARSER(PowerPC)
 #endif
diff --git a/lib/clang/include/llvm/Config/AsmPrinters.def b/lib/clang/include/llvm/Config/AsmPrinters.def
index b88e60ada874..2f45fe174db7 100644
--- a/lib/clang/include/llvm/Config/AsmPrinters.def
+++ b/lib/clang/include/llvm/Config/AsmPrinters.def
@@ -33,6 +33,9 @@ LLVM_ASM_PRINTER(ARM)
 #ifdef LLVM_TARGET_ENABLE_BPF
 LLVM_ASM_PRINTER(BPF)
 #endif
+#ifdef LLVM_TARGET_ENABLE_LOONGARCH
+LLVM_ASM_PRINTER(LoongArch)
+#endif
 #ifdef LLVM_TARGET_ENABLE_POWERPC
 LLVM_ASM_PRINTER(PowerPC)
 #endif
diff --git a/lib/clang/include/llvm/Config/Disassemblers.def b/lib/clang/include/llvm/Config/Disassemblers.def
index 72c0dcab0ab3..2530e01e635f 100644
--- a/lib/clang/include/llvm/Config/Disassemblers.def
+++ b/lib/clang/include/llvm/Config/Disassemblers.def
@@ -33,6 +33,9 @@ LLVM_DISASSEMBLER(ARM)
 #ifdef LLVM_TARGET_ENABLE_BPF
 LLVM_DISASSEMBLER(BPF)
 #endif
+#ifdef LLVM_TARGET_ENABLE_LOONGARCH
+LLVM_DISASSEMBLER(LoongArch)
+#endif
 #ifdef LLVM_TARGET_ENABLE_POWERPC
 LLVM_DISASSEMBLER(PowerPC)
 #endif
diff --git a/lib/clang/include/llvm/Config/Targets.def b/lib/clang/include/llvm/Config/Targets.def
index 725efe253ced..70ef796f5d0a 100644
--- a/lib/clang/include/llvm/Config/Targets.def
+++ b/lib/clang/include/llvm/Config/Targets.def
@@ -32,6 +32,9 @@ LLVM_TARGET(ARM)
 #ifdef LLVM_TARGET_ENABLE_BPF
 LLVM_TARGET(BPF)
 #endif
+#ifdef LLVM_TARGET_ENABLE_LOONGARCH
+LLVM_TARGET(LoongArch)
+#endif
 #ifdef LLVM_TARGET_ENABLE_POWERPC
 LLVM_TARGET(PowerPC)
 #endif
diff --git a/lib/clang/libllvm/Makefile b/lib/clang/libllvm/Makefile
index 9baa95c19242..8516573e6fdf 100644
--- a/lib/clang/libllvm/Makefile
+++ b/lib/clang/libllvm/Makefile
@@ -27,14 +27,15 @@ CFLAGS+=	-I${.OBJDIR}
 CFLAGS+=	-I${SRCTOP}/sys/contrib/zstd/lib
 
 .if ${MK_LLVM_TARGET_AARCH64} == "no" && ${MK_LLVM_TARGET_ARM} == "no" && \
-    ${MK_LLVM_TARGET_BPF} == "no" && ${MK_LLVM_TARGET_POWERPC} == "no" && \
-    ${MK_LLVM_TARGET_RISCV} == "no" && ${MK_LLVM_TARGET_X86} == "no"
+    ${MK_LLVM_TARGET_BPF} == "no" && ${MK_LLVM_TARGET_LOONGARCH} == "no" && \
+    ${MK_LLVM_TARGET_POWERPC} == "no" && ${MK_LLVM_TARGET_RISCV} == "no" && \
+    ${MK_LLVM_TARGET_X86} == "no"
 .error Please enable at least one of: MK_LLVM_TARGET_AARCH64,\
- MK_LLVM_TARGET_ARM, MK_LLVM_TARGET_BPF, MK_LLVM_TARGET_POWERPC,\
- MK_LLVM_TARGET_RISCV, or MK_LLVM_TARGET_X86
+ MK_LLVM_TARGET_ARM, MK_LLVM_TARGET_BPF, MK_LLVM_TARGET_LOONGARCH, \
+ MK_LLVM_TARGET_POWERPC, MK_LLVM_TARGET_RISCV, or MK_LLVM_TARGET_X86
 .endif
 
-.for arch in AArch64 ARM BPF PowerPC RISCV X86
+.for arch in AArch64 ARM BPF LoongArch PowerPC RISCV X86
 . if ${MK_LLVM_TARGET_${arch:tu}} != "no"
 CFLAGS+=	-I${LLVM_SRCS}/lib/Target/${arch}
 . endif
@@ -1758,6 +1759,36 @@ SRCS_MIN+=	Target/X86/X86VZeroUpper.cpp
 SRCS_MIN+=	Target/X86/X86WinEHState.cpp
 SRCS_MIN+=	Target/X86/X86WinEHUnwindV2.cpp
 .endif # MK_LLVM_TARGET_X86
+.if ${MK_LLVM_TARGET_LOONGARCH} != "no"
+SRCS_MIN+=	Target/LoongArch/AsmParser/LoongArchAsmParser.cpp
+SRCS_MIN+=	Target/LoongArch/Disassembler/LoongArchDisassembler.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchAsmPrinter.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchDeadRegisterDefinitions.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchExpandAtomicPseudoInsts.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchExpandPseudoInsts.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchFrameLowering.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchISelDAGToDAG.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchISelLowering.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchInstrInfo.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchMCInstLower.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchMergeBaseOffset.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchOptWInstrs.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchRegisterInfo.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchSubtarget.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchTargetMachine.cpp
+SRCS_MIN+=	Target/LoongArch/LoongArchTargetTransformInfo.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchBaseInfo.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchELFStreamer.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchInstPrinter.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchMCAsmInfo.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchMCCodeEmitter.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchMCTargetDesc.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchMatInt.cpp
+SRCS_MIN+=	Target/LoongArch/MCTargetDesc/LoongArchTargetStreamer.cpp
+SRCS_MIN+=	Target/LoongArch/TargetInfo/LoongArchTargetInfo.cpp
+.endif # MK_LLVM_TARGET_LOONGARCH
 SRCS_MIN+=	TargetParser/AArch64TargetParser.cpp
 SRCS_MIN+=	TargetParser/ARMTargetParser.cpp
 SRCS_MIN+=	TargetParser/ARMTargetParserCommon.cpp
@@ -2271,7 +2302,8 @@ beforebuild:
 
 # Note: some rules are superfluous, not every combination is valid.
 .for arch in \
-	AArch64/AArch64 ARM/ARM BPF/BPF PowerPC/PPC RISCV/RISCV X86/X86
+	AArch64/AArch64 ARM/ARM BPF/BPF LoongArch/LoongArch PowerPC/PPC \
+	RISCV/RISCV X86/X86
 . for hdr in \
 	AsmMatcher/-gen-asm-matcher \
 	AsmWriter/-gen-asm-writer \
@@ -2418,6 +2450,17 @@ TGHDRS+=	X86GenRegisterBank.inc
 TGHDRS+=	X86GenRegisterInfo.inc
 TGHDRS+=	X86GenSubtargetInfo.inc
 .endif # MK_LLVM_TARGET_X86
+.if ${MK_LLVM_TARGET_LOONGARCH} != "no"
+TGHDRS+=	LoongArchGenAsmMatcher.inc
+TGHDRS+=	LoongArchGenAsmWriter.inc
+TGHDRS+=	LoongArchGenDAGISel.inc
+TGHDRS+=	LoongArchGenDisassemblerTables.inc
+TGHDRS+=	LoongArchGenInstrInfo.inc
+TGHDRS+=	LoongArchGenMCCodeEmitter.inc
+TGHDRS+=	LoongArchGenMCPseudoLowering.inc
+TGHDRS+=	LoongArchGenRegisterInfo.inc
+TGHDRS+=	LoongArchGenSubtargetInfo.inc
+.endif # MK_LLVM_TARGET_LOONGARCH
 
 DEPENDFILES+=	${TGHDRS:C/$/.d/}
 DPSRCS+=	${TGHDRS}
diff --git a/lib/clang/llvm.build.mk b/lib/clang/llvm.build.mk
index d06a2f42479c..3e67200abc66 100644
--- a/lib/clang/llvm.build.mk
+++ b/lib/clang/llvm.build.mk
@@ -83,6 +83,12 @@ CFLAGS+=	-DLLVM_TARGET_ENABLE_RISCV
 LLVM_NATIVE_ARCH=	RISCV
 . endif
 .endif
+.if ${MK_LLVM_TARGET_LOONGARCH} != "no"
+CFLAGS+=	-DLLVM_TARGET_ENABLE_LOONGARCH
+. if ${MACHINE_CPUARCH} == "loongarch"
+LLVM_NATIVE_ARCH=	LoongArch
+. endif
+.endif
 .if ${MK_LLVM_TARGET_X86} != "no"
 CFLAGS+=	-DLLVM_TARGET_ENABLE_X86
 . if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
diff --git a/share/man/man5/src.conf.5 b/share/man/man5/src.conf.5
index 37747ac4fb53..46e489f30f8f 100644
--- a/share/man/man5/src.conf.5
+++ b/share/man/man5/src.conf.5
@@ -1,5 +1,5 @@
 .\" DO NOT EDIT-- this file is @generated by tools/build/options/makeman.
-.Dd September 24, 2026
+.Dd September 28, 2026
 .Dt SRC.CONF 5
 .Os
 .Sh NAME
@@ -1040,9 +1040,8 @@ The
 option should be used rather than this in most cases.
 .It Va WITH_LLVM_TARGET_BPF
 Build LLVM target support for BPF.
-The
-.Va LLVM_TARGET_ALL
-option should be used rather than this in most cases.
+.It Va WITH_LLVM_TARGET_LOONGARCH
+Build LLVM target support for LoongArch.
 .It Va WITHOUT_LLVM_TARGET_POWERPC
 Do not build LLVM target support for PowerPC.
 The
diff --git a/share/mk/src.opts.mk b/share/mk/src.opts.mk
index 8ca5fbd1873b..3e177d373c05 100644
--- a/share/mk/src.opts.mk
+++ b/share/mk/src.opts.mk
@@ -293,7 +293,7 @@ __DEFAULT_DEPENDENT_OPTIONS+=	LLVM_TARGET_${__llt:${__LLVM_TARGET_FILT}:tu}/LLVM
 .endif
 .endfor
 
-__DEFAULT_NO_OPTIONS+=LLVM_TARGET_BPF
+__DEFAULT_NO_OPTIONS+=LLVM_TARGET_BPF LLVM_TARGET_LOONGARCH
 
 .include <bsd.compiler.mk>
 
diff --git a/tools/build/options/WITHOUT_LLVM_TARGET_BPF b/tools/build/options/WITHOUT_LLVM_TARGET_BPF
new file mode 100644
index 000000000000..392e930d8008
--- /dev/null
+++ b/tools/build/options/WITHOUT_LLVM_TARGET_BPF
@@ -0,0 +1 @@
+Do not build LLVM target support for BPF.
diff --git a/tools/build/options/WITHOUT_LLVM_TARGET_LOONGARCH b/tools/build/options/WITHOUT_LLVM_TARGET_LOONGARCH
new file mode 100644
index 000000000000..e6d5d8be834f
--- /dev/null
+++ b/tools/build/options/WITHOUT_LLVM_TARGET_LOONGARCH
@@ -0,0 +1 @@
+Do not build LLVM target support for LoongArch.
diff --git a/tools/build/options/WITH_LLVM_TARGET_BPF b/tools/build/options/WITH_LLVM_TARGET_BPF
index 664cc85a52b9..45811be6174d 100644
--- a/tools/build/options/WITH_LLVM_TARGET_BPF
+++ b/tools/build/options/WITH_LLVM_TARGET_BPF
@@ -1,4 +1 @@
 Build LLVM target support for BPF.
-The
-.Va LLVM_TARGET_ALL
-option should be used rather than this in most cases.
diff --git a/tools/build/options/WITH_LLVM_TARGET_LOONGARCH b/tools/build/options/WITH_LLVM_TARGET_LOONGARCH
new file mode 100644
index 000000000000..157b18a9df9a
--- /dev/null
+++ b/tools/build/options/WITH_LLVM_TARGET_LOONGARCH
@@ -0,0 +1 @@
+Build LLVM target support for LoongArch.