From 7e21e1bfc1e9cf8031c55df2cc1f334deedce159 Mon Sep 17 00:00:00 2001
From: zhaojiale <zhaojiale@loongson.cn>
Date: Thu, 7 Dec 2023 06:21:08 +0800
Subject: [PATCH 1/2] llvmpipe: add loongarch64 basic support

Signed-off-by: zhaojiale <zhaojiale@loongson.cn>
---
 .../auxiliary/gallivm/lp_bld_debug.cpp        |  6 ++++++
 src/gallium/auxiliary/gallivm/lp_bld_debug.h  |  6 ++++++
 src/gallium/auxiliary/gallivm/lp_bld_init.c   | 20 +++++++++++++++++--
 src/util/detect_arch.h                        | 14 +++++++++++++
 5 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp b/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp
index 2918d38..eeed17d 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp
+++ b/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp
@@ -171,6 +171,12 @@ disassemble(const void* func, std::ostream &buffer)
       }
 #endif
 
+#if DETECT_ARCH_LOONGARCH64
+   if (Size == 4 && (*(uint32_t *)(bytes+pc) >> 26) == 0x13) {
+      break;
+   }
+#endif
+
       /*
        * Advance.
        */
diff --git a/src/gallium/auxiliary/gallivm/lp_bld_debug.h b/src/gallium/auxiliary/gallivm/lp_bld_debug.h
index a8db59b..30100aa 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_debug.h
+++ b/src/gallium/auxiliary/gallivm/lp_bld_debug.h
@@ -49,6 +49,12 @@
 #define GALLIVM_PERF_NO_OPT          (1 << 3)
 #define GALLIVM_PERF_NO_AOS_SAMPLING (1 << 4)
 
+#if DETECT_ARCH_LOONGARCH64
+#define GALLIVM_PERF_OPT_O1        (1 << 5)
+#define GALLIVM_PERF_OPT_O2        (1 << 6)
+#define GALLIVM_PERF_OPT_O3        (1 << 7)
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/src/gallium/auxiliary/gallivm/lp_bld_init.c b/src/gallium/auxiliary/gallivm/lp_bld_init.c
index 9750cf7..3c67c11 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_init.c
+++ b/src/gallium/auxiliary/gallivm/lp_bld_init.c
@@ -50,7 +50,7 @@
 #if LLVM_VERSION_MAJOR >= 7
 #include <llvm-c/Transforms/Utils.h>
 #endif
-#if LLVM_VERSION_MAJOR <= 8 && (DETECT_ARCH_AARCH64 || DETECT_ARCH_ARM || DETECT_ARCH_S390 || DETECT_ARCH_MIPS64)
+#if LLVM_VERSION_MAJOR <= 8 && (DETECT_ARCH_AARCH64 || DETECT_ARCH_ARM || DETECT_ARCH_S390 || DETECT_ARCH_MIPS64 || DETECT_ARCH_LOONGARCH64)
 #include <llvm-c/Transforms/IPO.h>
 #endif
 #include <llvm-c/Transforms/Coroutines.h>
@@ -64,6 +64,11 @@ static const struct debug_named_value lp_bld_perf_flags[] = {
    { "no_quad_lod", GALLIVM_PERF_NO_QUAD_LOD, "disable quad_lod optimization" },
    { "no_aos_sampling", GALLIVM_PERF_NO_AOS_SAMPLING, "disable aos sampling optimization" },
    { "nopt",   GALLIVM_PERF_NO_OPT, "disable optimization passes to speed up shader compilation" },
+#if DETECT_ARCH_LOONGARCH64
+   { "o3",   GALLIVM_PERF_OPT_O3, "enable aggressive optimization passes" },
+   { "o2",   GALLIVM_PERF_OPT_O2, "enable medium optimization passes" },
+   { "o1",   GALLIVM_PERF_OPT_O1, "enable less optimization passes" },
+#endif
    DEBUG_NAMED_VALUE_END
 };
 
@@ -141,7 +146,7 @@ create_pass_manager(struct gallivm_state *gallivm)
    }
 
 #if GALLIVM_HAVE_CORO == 1
-#if LLVM_VERSION_MAJOR <= 8 && (DETECT_ARCH_AARCH64 || DETECT_ARCH_ARM || DETECT_ARCH_S390 || DETECT_ARCH_MIPS64)
+#if LLVM_VERSION_MAJOR <= 8 && (DETECT_ARCH_AARCH64 || DETECT_ARCH_ARM || DETECT_ARCH_S390 || DETECT_ARCH_MIPS64 || DETECT_ARCH_LOONGARCH64)
    LLVMAddArgumentPromotionPass(gallivm->cgpassmgr);
    LLVMAddFunctionAttrsPass(gallivm->cgpassmgr);
 #endif
@@ -281,6 +286,17 @@ init_gallivm_engine(struct gallivm_state *gallivm)
          optlevel = Default;
       }
 
+#if DETECT_ARCH_LOONGARCH64
+      if (gallivm_perf & GALLIVM_PERF_OPT_O3)
+         optlevel = Aggressive;
+      else if (gallivm_perf & GALLIVM_PERF_OPT_O2)
+         optlevel = Default;
+      else if (gallivm_perf & GALLIVM_PERF_OPT_O1)
+         optlevel = Less;
+      else
+         optlevel = Default;
+#endif
+
       ret = lp_build_create_jit_compiler_for_module(&gallivm->engine,
                                                     &gallivm->code,
                                                     gallivm->cache,
diff --git a/src/util/detect_arch.h b/src/util/detect_arch.h
index 75fa7ed..c0f8cdb 100644
--- a/src/util/detect_arch.h
+++ b/src/util/detect_arch.h
@@ -97,6 +97,12 @@
 #define DETECT_ARCH_HPPA 1
 #endif
 
+#if defined(__loongarch_lp64) || defined(__loongarch64)
+#define DETECT_ARCH_LOONGARCH64 1
+#elif defined(__loongarch__)
+#define DETECT_ARCH_LOONGARCH 1
+#endif
+
 #if defined(__riscv)
 #define DETECT_ARCH_RISCV 1
 #if __riscv_xlen == 64
@@ -148,6 +154,14 @@
 #define DETECT_ARCH_HPPA 0
 #endif
 
+#ifndef DETECT_ARCH_LOONGARCH
+#define DETECT_ARCH_LOONGARCH 0
+#endif
+
+#ifndef DETECT_ARCH_LOONGARCH64
+#define DETECT_ARCH_LOONGARCH64 0
+#endif
+
 #ifndef DETECT_ARCH_RISCV
 #define DETECT_ARCH_RISCV 0
 #endif
-- 
2.43.0

