On 11/27/25 17:22, mengqinggang wrote:
[snip]
diff --git a/gcc/config/loongarch/loongarch-c.cc 
b/gcc/config/loongarch/loongarch-c.cc
index fc031a6fe90..6e2f8f43818 100644
--- a/gcc/config/loongarch/loongarch-c.cc
+++ b/gcc/config/loongarch/loongarch-c.cc
@@ -55,6 +55,11 @@ loongarch_define_unconditional_macros (cpp_reader *pfile)
        builtin_define ("__loongarch_grlen=64");
        builtin_define ("__loongarch64");
      }
+  else
+      builtin_define ("__loongarch_grlen=32");
+
+  if (TARGET_ABI_ILP32)
+      builtin_define ("__loongarch_ilp32");
if (TARGET_ABI_LP64)
      {

It would be great to indicate LA32R with a builtin macro too, something like "__loongarch_la32r" sounds great to me. I'm okay with spelling "reduced" fully out too.

Background: I just found it necessary while trying to port RocksDB's timestamp routine to LA32, which needs to handle the well-known behavior of counter wraparound without branching:

  rdcntvh.w HI1
  rdcntvl.w LO
  rdcntvh.w HI2
  xor HI1, HI1, HI2    // HI1 != 0 -> wrapped around
  masknez LO, LO, HI1  // LO = wrapped around ? 0 : LO

The problem is that "masknez" is absent in LA32R, and requires expansion:

  ...
  sltui HI1, HI1, 1  // HI1 = wrapped around ? 0 : 1
  sub HI1, $zero, HI1   // HI1 = wrapped around ? 0 : all ones
  and LO, LO, HI1       // LO = wrapped around ? 0 : LO

which means 2 additional cycles on top of 5.

Given that the instruction is present in LA32S (S for "standard"), and that in practice most if not all LA32 cores sold are LA32S-capable [^la32s], it can be beneficial to have the "LA32R flag" exposed to programmers, who can then bring more performance for the majority of LA32S deployments while preserving compatibility.

[^la32s]: After all, LA32R probably only got defined for educational purposes, because otherwise Loongson should already have made available some extremely small LA32R-only IP cores, to promote adoption.

--
Regards,
WANG "xen0n" Xuerui

Linux/LoongArch mailing list: https://lore.kernel.org/loongarch/
Unofficial Loongson OSS community: https://github.com/loongson-community

Reply via email to