From 6e82f232ece789939c4886754345a9eb3469cac6 Mon Sep 17 00:00:00 2001 From: Umesh Kalappa Date: Wed, 17 Sep 2025 05:48:57 +0000 Subject: [PATCH 1/4] RISCV: the builtins support for MIPS RV64 P8700 execution control instructions --- clang/include/clang/Basic/BuiltinsRISCV.td | 5 +++ .../include/clang/Basic/BuiltinsRISCVXMIPS.td | 27 ++++++++++++++ clang/test/CodeGen/builtins-riscv-mips.c | 35 +++++++++++++++++++ llvm/include/llvm/IR/IntrinsicsRISCV.td | 1 + llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td | 20 +++++++++++ llvm/lib/Target/RISCV/RISCVFeatures.td | 2 +- llvm/lib/Target/RISCV/RISCVInstrInfoXMips.td | 7 ++++ llvm/test/CodeGen/RISCV/xmips-exectl.ll | 35 +++++++++++++++++++ 8 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 clang/include/clang/Basic/BuiltinsRISCVXMIPS.td create mode 100644 clang/test/CodeGen/builtins-riscv-mips.c create mode 100644 llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td create mode 100644 llvm/test/CodeGen/RISCV/xmips-exectl.ll diff --git a/clang/include/clang/Basic/BuiltinsRISCV.td b/clang/include/clang/Basic/BuiltinsRISCV.td index 5927eaf80d57a..2dad5ede2d64b 100644 --- a/clang/include/clang/Basic/BuiltinsRISCV.td +++ b/clang/include/clang/Basic/BuiltinsRISCV.td @@ -162,3 +162,8 @@ include "clang/Basic/BuiltinsRISCVXCV.td" // XAndes extensions. //===----------------------------------------------------------------------===// include "clang/Basic/BuiltinsRISCVXAndes.td" + +//===----------------------------------------------------------------------===// +// MIPS extensions. +//===----------------------------------------------------------------------===// +include "clang/Basic/BuiltinsRISCVXMIPS.td" diff --git a/clang/include/clang/Basic/BuiltinsRISCVXMIPS.td b/clang/include/clang/Basic/BuiltinsRISCVXMIPS.td new file mode 100644 index 0000000000000..28cf6c9bb1707 --- /dev/null +++ b/clang/include/clang/Basic/BuiltinsRISCVXMIPS.td @@ -0,0 +1,27 @@ +//==- BuiltinsRISCVXMIPS.td - RISC-V MIPS Builtin database ----*- C++ -*-==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines the MIPS-specific builtin function database. Users of +// this file must define the BUILTIN macro to make use of this information. +// +//===----------------------------------------------------------------------===// + +class RISCVXMIPSBuiltin : TargetBuiltin { + let Spellings = ["__builtin_riscv_" # NAME]; + let Prototype = prototype; + let Features = features; +} + +//===----------------------------------------------------------------------===// +// MIPS execution control extensions . +//===----------------------------------------------------------------------===// +let Attributes = [NoThrow, Const] in { +def mips_pause : RISCVXMIPSBuiltin<"void()", "xmipsexectl">; +def mips_ehb : RISCVXMIPSBuiltin<"void()", "xmipsexectl">; +def mips_ihb : RISCVXMIPSBuiltin<"void()", "xmipsexectl">; +} \ No newline at end of file diff --git a/clang/test/CodeGen/builtins-riscv-mips.c b/clang/test/CodeGen/builtins-riscv-mips.c new file mode 100644 index 0000000000000..3c6c9b994f5d2 --- /dev/null +++ b/clang/test/CodeGen/builtins-riscv-mips.c @@ -0,0 +1,35 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 6 +// RUN: %clang_cc1 -triple riscv32-linux-elf -O3 -target-feature +xmipsexectl -emit-llvm -o - %s | FileCheck %s RISCV32 + +// CHECK-LABEL: define dso_local void @test_mips_pause( +// CHECK-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: tail call void @llvm.riscv.mips.pause() +// CHECK-NEXT: ret void +// +void test_mips_pause() +{ + __builtin_riscv_mips_pause(); +} + +// CHECK-LABEL: define dso_local void @test_mips_ehb( +// CHECK-SAME: ) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: tail call void @llvm.riscv.mips.ehb() +// CHECK-NEXT: ret void +// +void test_mips_ehb() +{ + __builtin_riscv_mips_ehb(); +} + +// CHECK-LABEL: define dso_local void @test_mips_ihb( +// CHECK-SAME: ) local_unnamed_addr #[[ATTR0]] { +// CHECK-NEXT: [[ENTRY:.*:]] +// CHECK-NEXT: tail call void @llvm.riscv.mips.ihb() +// CHECK-NEXT: ret void +// +void test_mips_ihb() +{ + __builtin_riscv_mips_ihb(); +} diff --git a/llvm/include/llvm/IR/IntrinsicsRISCV.td b/llvm/include/llvm/IR/IntrinsicsRISCV.td index 878f7b3194830..bd2c95c5af587 100644 --- a/llvm/include/llvm/IR/IntrinsicsRISCV.td +++ b/llvm/include/llvm/IR/IntrinsicsRISCV.td @@ -1949,3 +1949,4 @@ include "llvm/IR/IntrinsicsRISCVXTHead.td" include "llvm/IR/IntrinsicsRISCVXsf.td" include "llvm/IR/IntrinsicsRISCVXCV.td" include "llvm/IR/IntrinsicsRISCVXAndes.td" +include "llvm/IR/IntrinsicsRISCVXMIPS.td" diff --git a/llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td b/llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td new file mode 100644 index 0000000000000..ced96ac2e10a7 --- /dev/null +++ b/llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td @@ -0,0 +1,20 @@ +//===- IntrinsicsRISCVXMIPS.td - Defines MIPS intrinsics -------*- tablegen -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines all of the MIPS specific intrinsics for RISCV. +// +//===----------------------------------------------------------------------===// + +let TargetPrefix = "riscv" in { + def int_riscv_mips_pause : ClangBuiltin<"__builtin_riscv_mips_pause">, + Intrinsic<[], [], [IntrNoMem, IntrHasSideEffects]>; + def int_riscv_mips_ehb : ClangBuiltin<"__builtin_riscv_mips_ehb">, + Intrinsic<[], [], [IntrNoMem, IntrHasSideEffects]>; + def int_riscv_mips_ihb : ClangBuiltin<"__builtin_riscv_mips_ihb">, + Intrinsic<[], [], [IntrNoMem, IntrHasSideEffects]>; +} diff --git a/llvm/lib/Target/RISCV/RISCVFeatures.td b/llvm/lib/Target/RISCV/RISCVFeatures.td index 95703e33926c5..6433fe888b894 100644 --- a/llvm/lib/Target/RISCV/RISCVFeatures.td +++ b/llvm/lib/Target/RISCV/RISCVFeatures.td @@ -1426,7 +1426,7 @@ def NoVendorXMIPSCBOP : Predicate<"!Subtarget->hasVendorXMIPSCBOP()">; def FeatureVendorXMIPSEXECTL : RISCVExtension<1, 0, "MIPS execution control">; def HasVendorXMIPSEXECTL - : Predicate<"Subtarget->hasVendorXMIPSEXT()">, + : Predicate<"Subtarget->hasVendorXMIPSEXECTL()">, AssemblerPredicate<(all_of FeatureVendorXMIPSEXECTL), "'Xmipsexectl' (MIPS execution control)">; diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoXMips.td b/llvm/lib/Target/RISCV/RISCVInstrInfoXMips.td index d615094329b28..115ab38e5d4c1 100644 --- a/llvm/lib/Target/RISCV/RISCVInstrInfoXMips.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoXMips.td @@ -143,6 +143,13 @@ let Predicates = [HasVendorXMIPSEXECTL], DecoderNamespace = "XMIPS" in { def MIPS_PAUSE : MIPSExtInst_ri<0b000101, "mips.pause">; } +let Predicates = [HasVendorXMIPSEXECTL] in { + // Intrinsics + def : Pat<(int_riscv_mips_pause), (MIPS_PAUSE)>; + def : Pat<(int_riscv_mips_ihb), (MIPS_IHB)>; + def : Pat<(int_riscv_mips_ehb), (MIPS_EHB)>; +} + let Predicates = [HasVendorXMIPSCBOP], DecoderNamespace = "XMIPS" in { def MIPS_PREF : Mips_prefetch_ri<(outs), (ins GPR:$rs1, uimm9:$imm9, uimm5:$hint), "mips.pref", "$hint, ${imm9}(${rs1})">, diff --git a/llvm/test/CodeGen/RISCV/xmips-exectl.ll b/llvm/test/CodeGen/RISCV/xmips-exectl.ll new file mode 100644 index 0000000000000..bc0c96e7301b2 --- /dev/null +++ b/llvm/test/CodeGen/RISCV/xmips-exectl.ll @@ -0,0 +1,35 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --tool /home/ukalappa/llvm/github/build/bin/llc --version 6 +; RUN: llc -mtriple=riscv64 -mcpu=mips-p8700 -O3 -verify-machineinstrs < %s \ +; RUN: | FileCheck -check-prefix=MIPS %s + +target datalayout = "e-m:e-p:32:32-i64:64-n32-S128" + +define dso_local void @test_mips_pause() local_unnamed_addr { +; MIPS-LABEL: test_mips_pause: +; MIPS: # %bb.0: # %entry +; MIPS-NEXT: mips.pause +; MIPS-NEXT: ret +entry: + tail call void @llvm.riscv.mips.pause() + ret void +} + +define dso_local void @test_mips_ehb() local_unnamed_addr { +; MIPS-LABEL: test_mips_ehb: +; MIPS: # %bb.0: # %entry +; MIPS-NEXT: mips.ehb +; MIPS-NEXT: ret +entry: + tail call void @llvm.riscv.mips.ehb() + ret void +} + +define dso_local void @test_mips_ihb() local_unnamed_addr { +; MIPS-LABEL: test_mips_ihb: +; MIPS: # %bb.0: # %entry +; MIPS-NEXT: mips.ihb +; MIPS-NEXT: ret +entry: + tail call void @llvm.riscv.mips.ihb() + ret void +} From fbcef5fa4bb0f5e19aee101c4fdbc745cff699dd Mon Sep 17 00:00:00 2001 From: Umesh Kalappa Date: Wed, 17 Sep 2025 06:13:10 +0000 Subject: [PATCH 2/4] Testcase fix. --- clang/test/CodeGen/builtins-riscv-mips.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/CodeGen/builtins-riscv-mips.c b/clang/test/CodeGen/builtins-riscv-mips.c index 3c6c9b994f5d2..55ce50a85ab38 100644 --- a/clang/test/CodeGen/builtins-riscv-mips.c +++ b/clang/test/CodeGen/builtins-riscv-mips.c @@ -1,5 +1,5 @@ // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 6 -// RUN: %clang_cc1 -triple riscv32-linux-elf -O3 -target-feature +xmipsexectl -emit-llvm -o - %s | FileCheck %s RISCV32 +// RUN: %clang_cc1 -triple riscv32-linux-elf -O3 -target-feature +xmipsexectl -emit-llvm -o - %s | FileCheck %s // CHECK-LABEL: define dso_local void @test_mips_pause( // CHECK-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] { From 5525ab5991e6e32406f6eb1f3be142545defb95a Mon Sep 17 00:00:00 2001 From: Umesh Kalappa Date: Thu, 18 Sep 2025 04:01:52 +0000 Subject: [PATCH 3/4] Updated the changes by adopting the suggestions. --- clang/include/clang/Basic/BuiltinsRISCVXMIPS.td | 16 +++++----------- llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td | 4 ++-- llvm/test/CodeGen/RISCV/xmips-exectl.ll | 8 ++++---- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/clang/include/clang/Basic/BuiltinsRISCVXMIPS.td b/clang/include/clang/Basic/BuiltinsRISCVXMIPS.td index 28cf6c9bb1707..07fb68ad0881f 100644 --- a/clang/include/clang/Basic/BuiltinsRISCVXMIPS.td +++ b/clang/include/clang/Basic/BuiltinsRISCVXMIPS.td @@ -11,17 +11,11 @@ // //===----------------------------------------------------------------------===// -class RISCVXMIPSBuiltin : TargetBuiltin { - let Spellings = ["__builtin_riscv_" # NAME]; - let Prototype = prototype; - let Features = features; -} - //===----------------------------------------------------------------------===// -// MIPS execution control extensions . +// MIPS execution control extensions. //===----------------------------------------------------------------------===// let Attributes = [NoThrow, Const] in { -def mips_pause : RISCVXMIPSBuiltin<"void()", "xmipsexectl">; -def mips_ehb : RISCVXMIPSBuiltin<"void()", "xmipsexectl">; -def mips_ihb : RISCVXMIPSBuiltin<"void()", "xmipsexectl">; -} \ No newline at end of file +def mips_pause : RISCVBuiltin<"void()", "xmipsexectl">; +def mips_ehb : RISCVBuiltin<"void()", "xmipsexectl">; +def mips_ihb : RISCVBuiltin<"void()", "xmipsexectl">; +} diff --git a/llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td b/llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td index ced96ac2e10a7..d231feb7d6618 100644 --- a/llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td +++ b/llvm/include/llvm/IR/IntrinsicsRISCVXMIPS.td @@ -1,4 +1,4 @@ -//===- IntrinsicsRISCVXMIPS.td - Defines MIPS intrinsics -------*- tablegen -*-===// +//===- IntrinsicsRISCVXMIPS.td - MIPS intrinsics -------*- tablegen -*----===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// // -// This file defines all of the MIPS specific intrinsics for RISCV. +// This file defines all of the MIPS specific intrinsics for RISC-V. // //===----------------------------------------------------------------------===// diff --git a/llvm/test/CodeGen/RISCV/xmips-exectl.ll b/llvm/test/CodeGen/RISCV/xmips-exectl.ll index bc0c96e7301b2..a013e01c7cdc1 100644 --- a/llvm/test/CodeGen/RISCV/xmips-exectl.ll +++ b/llvm/test/CodeGen/RISCV/xmips-exectl.ll @@ -1,10 +1,10 @@ -; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --tool /home/ukalappa/llvm/github/build/bin/llc --version 6 +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6 ; RUN: llc -mtriple=riscv64 -mcpu=mips-p8700 -O3 -verify-machineinstrs < %s \ ; RUN: | FileCheck -check-prefix=MIPS %s target datalayout = "e-m:e-p:32:32-i64:64-n32-S128" -define dso_local void @test_mips_pause() local_unnamed_addr { +define void @test_mips_pause() { ; MIPS-LABEL: test_mips_pause: ; MIPS: # %bb.0: # %entry ; MIPS-NEXT: mips.pause @@ -14,7 +14,7 @@ entry: ret void } -define dso_local void @test_mips_ehb() local_unnamed_addr { +define void @test_mips_ehb() { ; MIPS-LABEL: test_mips_ehb: ; MIPS: # %bb.0: # %entry ; MIPS-NEXT: mips.ehb @@ -24,7 +24,7 @@ entry: ret void } -define dso_local void @test_mips_ihb() local_unnamed_addr { +define void @test_mips_ihb() { ; MIPS-LABEL: test_mips_ihb: ; MIPS: # %bb.0: # %entry ; MIPS-NEXT: mips.ihb From 9aa3bd380689370fe4ca6ee691919edf5c58130d Mon Sep 17 00:00:00 2001 From: Umesh Kalappa Date: Fri, 19 Sep 2025 08:25:10 +0000 Subject: [PATCH 4/4] Added Headers to wrapped around the mips builtins. --- clang/lib/Headers/CMakeLists.txt | 1 + clang/lib/Headers/riscv_mips.h | 34 +++++++++++++++++++ .../CodeGen/{ => RISCV}/builtins-riscv-mips.c | 8 +++-- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 clang/lib/Headers/riscv_mips.h rename clang/test/CodeGen/{ => RISCV}/builtins-riscv-mips.c (91%) diff --git a/clang/lib/Headers/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt index dd52498bbef4c..32a6be88abc20 100644 --- a/clang/lib/Headers/CMakeLists.txt +++ b/clang/lib/Headers/CMakeLists.txt @@ -131,6 +131,7 @@ set(riscv_files riscv_ntlh.h sifive_vector.h andes_vector.h + riscv_mips.h ) set(spirv_files diff --git a/clang/lib/Headers/riscv_mips.h b/clang/lib/Headers/riscv_mips.h new file mode 100644 index 0000000000000..124a989280ed4 --- /dev/null +++ b/clang/lib/Headers/riscv_mips.h @@ -0,0 +1,34 @@ +//===----- riscv_mips.h - RISC-V MIPS Intrinsic definitions +//----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef __RISCV_MIPS_H +#define __RISCV_MIPS_H + +#if !defined(__riscv) +#error "This header is only meant to be used on riscv architecture" +#endif + +#define __DEFAULT_FN_ATTRS \ + __attribute__((__always_inline__, __nodebug__, __target__("xmipsexectl"))) + +static __inline__ void __DEFAULT_FN_ATTRS __mips_pause() { + __builtin_riscv_mips_pause(); +} + +static __inline__ void __DEFAULT_FN_ATTRS __mips_ehb() { + __builtin_riscv_mips_ehb(); +} + +static __inline__ void __DEFAULT_FN_ATTRS __mips_ihb() { + __builtin_riscv_mips_ihb(); +} + +#undef __DEFAULT_FN_ATTRS + +#endif diff --git a/clang/test/CodeGen/builtins-riscv-mips.c b/clang/test/CodeGen/RISCV/builtins-riscv-mips.c similarity index 91% rename from clang/test/CodeGen/builtins-riscv-mips.c rename to clang/test/CodeGen/RISCV/builtins-riscv-mips.c index 55ce50a85ab38..b20ab801428c4 100644 --- a/clang/test/CodeGen/builtins-riscv-mips.c +++ b/clang/test/CodeGen/RISCV/builtins-riscv-mips.c @@ -1,6 +1,8 @@ // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 6 // RUN: %clang_cc1 -triple riscv32-linux-elf -O3 -target-feature +xmipsexectl -emit-llvm -o - %s | FileCheck %s +#include + // CHECK-LABEL: define dso_local void @test_mips_pause( // CHECK-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] { // CHECK-NEXT: [[ENTRY:.*:]] @@ -9,7 +11,7 @@ // void test_mips_pause() { - __builtin_riscv_mips_pause(); + __mips_pause(); } // CHECK-LABEL: define dso_local void @test_mips_ehb( @@ -20,7 +22,7 @@ void test_mips_pause() // void test_mips_ehb() { - __builtin_riscv_mips_ehb(); + __mips_ehb(); } // CHECK-LABEL: define dso_local void @test_mips_ihb( @@ -31,5 +33,5 @@ void test_mips_ehb() // void test_mips_ihb() { - __builtin_riscv_mips_ihb(); + __mips_ihb(); }