Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RISCV] Add riscv_atomic.h and Zawrs builtins #96283

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/include/clang/Basic/BuiltinsRISCV.td
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ let Features = "zihintntl", Attributes = [CustomTypeChecking] in {
def ntl_load : RISCVBuiltin<"void(...)">;
def ntl_store : RISCVBuiltin<"void(...)">;
} // Features = "zihintntl", Attributes = [CustomTypeChecking]

//===----------------------------------------------------------------------===//
// Zawrs extension.
//===----------------------------------------------------------------------===//
let Features = "zawrs" in {
def wrs_nto : RISCVBuiltin<"void()">;
def wrs_sto : RISCVBuiltin<"void()">;
} // Features = "zawrs"
8 changes: 8 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21834,6 +21834,14 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
ID = Intrinsic::riscv_sm3p1;
break;

// Zawrs
case RISCV::BI__builtin_riscv_wrs_nto:
ID = Intrinsic::riscv_wrs_nto;
break;
case RISCV::BI__builtin_riscv_wrs_sto:
ID = Intrinsic::riscv_wrs_sto;
break;

// Zihintntl
case RISCV::BI__builtin_riscv_ntl_load: {
llvm::Type *ResTy = ConvertType(E->getType());
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ set(ppc_htm_files
)

set(riscv_files
riscv_atomic.h
riscv_bitmanip.h
riscv_crypto.h
riscv_ntlh.h
Expand Down
18 changes: 18 additions & 0 deletions clang/lib/Headers/riscv_atomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*===---- riscv_atomic.h - RISC-V atomic intrinsics ------------------------===
*
* 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_ATOMIC_H
#define __RISCV_ATOMIC_H

#ifdef __riscv_zawrs
#define __riscv_wrs_nto __builtin_riscv_wrs_nto
#define __riscv_wrs_sto __builtin_riscv_wrs_sto
#endif

#endif
42 changes: 42 additions & 0 deletions clang/test/CodeGen/RISCV/atomics-intrinsics/zawrs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 2
// REQUIRES: riscv-registered-target
// RUN: %clang_cc1 -triple riscv32 -target-feature +zawrs -disable-O0-optnone \
// RUN: -emit-llvm %s -o - | opt -S -passes=mem2reg | \
// RUN: FileCheck --check-prefixes=CHECK-RV32 %s
// RUN: %clang_cc1 -triple riscv64 -target-feature +zawrs -disable-O0-optnone \
// RUN: -emit-llvm %s -o - | opt -S -passes=mem2reg | \
// RUN: FileCheck --check-prefix=CHECK-RV64 %s

#include <riscv_atomic.h>

// CHECK-RV32-LABEL: define dso_local void @zawrs_nto
// CHECK-RV32-SAME: () #[[ATTR0:[0-9]+]] {
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: call void @llvm.riscv.wrs.nto()
// CHECK-RV32-NEXT: ret void
//
// CHECK-RV64-LABEL: define dso_local void @zawrs_nto
// CHECK-RV64-SAME: () #[[ATTR0:[0-9]+]] {
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: call void @llvm.riscv.wrs.nto()
// CHECK-RV64-NEXT: ret void
//
void zawrs_nto() {
__riscv_wrs_nto();
}

// CHECK-RV32-LABEL: define dso_local void @zawrs_sto
// CHECK-RV32-SAME: () #[[ATTR0]] {
// CHECK-RV32-NEXT: entry:
// CHECK-RV32-NEXT: call void @llvm.riscv.wrs.sto()
// CHECK-RV32-NEXT: ret void
//
// CHECK-RV64-LABEL: define dso_local void @zawrs_sto
// CHECK-RV64-SAME: () #[[ATTR0]] {
// CHECK-RV64-NEXT: entry:
// CHECK-RV64-NEXT: call void @llvm.riscv.wrs.sto()
// CHECK-RV64-NEXT: ret void
//
void zawrs_sto() {
__riscv_wrs_sto();
}
10 changes: 10 additions & 0 deletions llvm/include/llvm/IR/IntrinsicsRISCV.td
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ let TargetPrefix = "riscv" in {
[IntrNoMem, IntrSpeculatable, ImmArg<ArgIndex<2>>]>;
} // TargetPrefix = "riscv"

//===----------------------------------------------------------------------===//
// 'Zawrs' (Wait on Reservation Set)

let TargetPrefix = "riscv" in {
def int_riscv_wrs_nto
: DefaultAttrsIntrinsic<[], [], [IntrHasSideEffects]>;
def int_riscv_wrs_sto
: DefaultAttrsIntrinsic<[], [], [IntrHasSideEffects]>;
} // TargetPrefix = "riscv"

//===----------------------------------------------------------------------===//
// Vectors

Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Target/RISCV/RISCVInstrInfoZa.td
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ defm : AMOCASPat<"atomic_cmp_swap_i64", "AMOCAS_D_RV64", i64, [IsRV64]>;
// Zawrs (Wait-on-Reservation-Set)
//===----------------------------------------------------------------------===//

let hasSideEffects = 1, mayLoad = 0, mayStore = 0 in
let hasSideEffects = 1, mayLoad = 1, mayStore = 1 in
class WRSInst<bits<12> funct12, string opcodestr>
: RVInstI<0b000, OPC_SYSTEM, (outs), (ins), opcodestr, ""> {
let rs1 = 0;
Expand All @@ -134,6 +134,9 @@ class WRSInst<bits<12> funct12, string opcodestr>
let Predicates = [HasStdExtZawrs] in {
def WRS_NTO : WRSInst<0b000000001101, "wrs.nto">, Sched<[]>;
def WRS_STO : WRSInst<0b000000011101, "wrs.sto">, Sched<[]>;

def : Pat<(int_riscv_wrs_nto), (WRS_NTO)>;
def : Pat<(int_riscv_wrs_sto), (WRS_NTO)>;
} // Predicates = [HasStdExtZawrs]

//===----------------------------------------------------------------------===//
Expand Down
33 changes: 33 additions & 0 deletions llvm/test/CodeGen/RISCV/zawrs-intrinsic.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=riscv32 -mattr=+zawrs -verify-machineinstrs < %s \
; RUN: | FileCheck %s -check-prefix=RV32
; RUN: llc -mtriple=riscv64 -mattr=+zawrs -verify-machineinstrs < %s \
; RUN: | FileCheck %s -check-prefix=RV64

define void @wrs_nto() {
; RV32-LABEL: wrs_nto:
; RV32: # %bb.0:
; RV32-NEXT: wrs.nto
; RV32-NEXT: ret
;
; RV64-LABEL: wrs_nto:
; RV64: # %bb.0:
; RV64-NEXT: wrs.nto
; RV64-NEXT: ret
call void @llvm.riscv.wrs.nto()
ret void
}

define void @wrs_sto() {
; RV32-LABEL: wrs_sto:
; RV32: # %bb.0:
; RV32-NEXT: wrs.nto
; RV32-NEXT: ret
;
; RV64-LABEL: wrs_sto:
; RV64: # %bb.0:
; RV64-NEXT: wrs.nto
; RV64-NEXT: ret
call void @llvm.riscv.wrs.sto()
ret void
}
Loading