Skip to content

Commit 9f6c005

Browse files
[MLIR][VCIX] Support VCIX intrinsics in LLVMIR dialect (#75875)
The changeset extends LLVMIR intrinsics with VCIX intrinsics. The VCIX intrinsics allow MLIR users to interact with RISC-V co-processors that are compatible with `XSfvcp` extension Source: https://www.sifive.com/document-file/sifive-vector-coprocessor-interface-vcix-software
1 parent caf537e commit 9f6c005

File tree

19 files changed

+1222
-0
lines changed

19 files changed

+1222
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,11 @@ mlir_tablegen(ROCDLConversions.inc -gen-llvmir-conversions)
7171
mlir_tablegen(ROCDLOpsAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=rocdl)
7272
mlir_tablegen(ROCDLOpsAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=rocdl)
7373
add_public_tablegen_target(MLIRROCDLConversionsIncGen)
74+
75+
add_mlir_dialect(VCIXOps vcix)
76+
add_mlir_doc(VCIXOps VCIXDialect Dialects/ -gen-dialect-doc -dialect=vcix)
77+
set(LLVM_TARGET_DEFINITIONS VCIXOps.td)
78+
mlir_tablegen(VCIXConversions.inc -gen-llvmir-conversions)
79+
mlir_tablegen(VCIXOpsAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=vcix)
80+
mlir_tablegen(VCIXOpsAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=vcix)
81+
add_public_tablegen_target(MLIRVCIXConversionsIncGen)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- VCIXDialect.h - MLIR VCIX IR dialect -------------------*- C++ ---*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// The file defines the basic operations for the VCIX dialect.
10+
//
11+
// The SiFive Vector Coprocessor Interface (VCIX) provides a flexible mechanism
12+
// to extend application processors with custom coprocessors and
13+
// variable-latency arithmetic units. The interface offers throughput comparable
14+
// to that of standard RISC-V vector instructions. To accelerate performance,
15+
// system designers may use VCIX as a low-latency, high-throughput interface to
16+
// a coprocessor
17+
//
18+
// https://www.sifive.com/document-file/sifive-vector-coprocessor-interface-vcix-software
19+
//
20+
//===----------------------------------------------------------------------===//
21+
22+
#ifndef MLIR_DIALECT_LLVMIR_VCIXDIALECT_H_
23+
#define MLIR_DIALECT_LLVMIR_VCIXDIALECT_H_
24+
25+
#include "mlir/Bytecode/BytecodeOpInterface.h"
26+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
27+
#include "mlir/IR/Dialect.h"
28+
#include "mlir/IR/OpDefinition.h"
29+
#include "mlir/Interfaces/SideEffectInterfaces.h"
30+
31+
///// Ops /////
32+
#define GET_ATTRDEF_CLASSES
33+
#include "mlir/Dialect/LLVMIR/VCIXOpsAttributes.h.inc"
34+
35+
#define GET_OP_CLASSES
36+
#include "mlir/Dialect/LLVMIR/VCIXOps.h.inc"
37+
38+
#include "mlir/Dialect/LLVMIR/VCIXOpsDialect.h.inc"
39+
40+
#endif /* MLIR_DIALECT_LLVMIR_VCIXDIALECT_H_ */
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//===-- VCIX.td - VCIX dialect operation definitions *- tablegen -*--------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// The file defines the basic operations for the VCIX dialect.
9+
//
10+
// The SiFive Vector Coprocessor Interface (VCIX) provides a flexible mechanism
11+
// to extend application processors with custom coprocessors and
12+
// variable-latency arithmetic units. The interface offers throughput comparable
13+
// to that of standard RISC-V vector instructions. To accelerate performance,
14+
// system designers may use VCIX as a low-latency, high-throughput interface to
15+
// a coprocessor
16+
//
17+
// https://www.sifive.com/document-file/sifive-vector-coprocessor-interface-vcix-software
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#ifndef VCIXIR_OPS
22+
23+
include "mlir/IR/OpBase.td"
24+
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
25+
26+
//===----------------------------------------------------------------------===//
27+
// VCIX dialect definition.
28+
//===----------------------------------------------------------------------===//
29+
30+
def VCIX_Dialect : Dialect {
31+
let name = "vcix";
32+
let cppNamespace = "::mlir::vcix";
33+
let dependentDialects = ["LLVM::LLVMDialect"];
34+
let description = [{
35+
The SiFive Vector Coprocessor Interface (VCIX) provides a flexible mechanism
36+
to extend application processors with custom coprocessors and
37+
variable-latency arithmetic units. The interface offers throughput comparable
38+
to that of standard RISC-V vector instructions. To accelerate performance,
39+
system designers may use VCIX as a low-latency, high-throughput interface to
40+
a coprocessor
41+
42+
https://www.sifive.com/document-file/sifive-vector-coprocessor-interface-vcix-software
43+
}];
44+
}
45+
46+
// Special version for intrinsic version where int attr is zext to i32 or i64
47+
// depending on xlen of the target.
48+
def VCIX_VectorOrScalar
49+
: AnyTypeOf<[LLVM_AnyVector, I<64>, I<32>, F<16>, F<32>, F<64>]>;
50+
def VCIX_OpcodeAttr : AnyAttrOf<[I32Attr, I64Attr]>;
51+
def VCIX_Register : AnyTypeOf<[I32, I64]>;
52+
def VCIX_ImmAttr : AnyAttrOf<[I32Attr, I64Attr]>;
53+
def VCIX_VL : AnyTypeOf<[I<64>, I<32>]>;
54+
55+
class VCIX_Op<string mnemonic, list<Trait> traits = []>
56+
: LLVM_OpBase<VCIX_Dialect, mnemonic, traits> {
57+
}
58+
59+
def VCIX_BinaryImmOp : VCIX_Op<"v.iv">,
60+
Results<(outs LLVM_AnyVector: $res)>,
61+
Arguments<(ins VCIX_OpcodeAttr: $opcode,
62+
LLVM_AnyVector: $vs2,
63+
VCIX_ImmAttr: $imm,
64+
Optional<VCIX_VL>: $vl)> {
65+
let summary = "Binary VCIX operation with an immediate second operand";
66+
let description = [{
67+
Binary VCIX operation with an immediate second operand.
68+
69+
Correponds to:
70+
|Mnemonic|funct6|vm|rs2|rs1|funct3|rd|Destination|Sources|
71+
|--|--|--|--|--|--|--|--|--|
72+
|sf.vc.v.iv|0010--|0|vs2|simm|011|vd|vector vd| simm[4:0] vector vs2|
73+
}];
74+
75+
string llvmBuilder = [{
76+
llvm::Type *xlen =getXlenType($opcode, moduleTranslation);
77+
llvm::Value *opcodeConst = mlir::LLVM::detail::getLLVMConstant(
78+
xlen, $opcode, $_location, moduleTranslation);
79+
llvm::Value *immConst = mlir::LLVM::detail::getLLVMConstant(
80+
xlen, $imm, $_location, moduleTranslation);
81+
VectorType vt = op.getResult().getType().cast<VectorType>();
82+
llvm::Value *vl =
83+
createVL(builder, $vl, vt, xlen, $_location, moduleTranslation);
84+
$res = createIntrinsicCall(
85+
builder, llvm::Intrinsic::riscv_sf_vc_v_iv_se,
86+
{opcodeConst, $vs2, immConst, vl},
87+
{$_resultType, xlen, $vs2->getType(), xlen, xlen});
88+
}];
89+
}
90+
91+
def VCIX_BinaryOp : VCIX_Op<"v.sv">,
92+
Results<(outs LLVM_AnyVector: $res)>,
93+
Arguments<(ins VCIX_OpcodeAttr: $opcode,
94+
LLVM_AnyVector: $vs2,
95+
VCIX_VectorOrScalar: $op,
96+
Optional<VCIX_VL>: $vl)> {
97+
let summary = "Binary VCIX operation";
98+
let description = [{
99+
Binary VCIX operation with an integer scalar, or floating pointer scalar or
100+
vector second operand.
101+
102+
Correponds to:
103+
|Mnemonic|funct6|vm|rs2|rs1|funct3|rd|Destination| Sources|
104+
|--|--|--|--|--|--|--|--|--|--|
105+
|sf.vc.v.vv|0010--|0|vs2|vs1|000|vd|vector vd|vector vs1, vector vs|
106+
|sf.vc.v.xv|0010--|0|vs2|xs1|100|vd|vector vd|scalar xs1, vector vs2|
107+
|sf.vc.v.fv|0010--|0|vs2|fs1|101|vd|vector vd|scalar fs1, vector vs2|
108+
}];
109+
110+
string llvmBuilder = [{
111+
llvm::Type *xlen = getXlenType($opcode, moduleTranslation);
112+
llvm::Value *opcodeConst = mlir::LLVM::detail::getLLVMConstant(
113+
xlen, $opcode, $_location, moduleTranslation);
114+
llvm::Intrinsic::ID id;
115+
llvm::Type *opType = $op->getType();
116+
if (opType->isVectorTy()) {
117+
id = llvm::Intrinsic::riscv_sf_vc_v_vv_se;
118+
} else if (opType->isIntegerTy()) {
119+
id = llvm::Intrinsic::riscv_sf_vc_v_xv_se;
120+
} else {
121+
id = llvm::Intrinsic::riscv_sf_vc_v_fv_se;
122+
}
123+
VectorType vt = op.getResult().getType().cast<VectorType>();
124+
llvm::Value *vl =
125+
createVL(builder, $vl, vt, xlen, $_location, moduleTranslation);
126+
$res = createIntrinsicCall(
127+
builder, id, {opcodeConst, $vs2, $op, vl},
128+
{$_resultType, xlen, $vs2->getType(), $op->getType(), xlen});
129+
}];
130+
}
131+
132+
#endif // VCIXIR_OPS

mlir/include/mlir/Target/LLVMIR/Dialect/All.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
2929
#include "mlir/Target/LLVMIR/Dialect/ROCDL/ROCDLToLLVMIRTranslation.h"
3030
#include "mlir/Target/LLVMIR/Dialect/SPIRV/SPIRVToLLVMIRTranslation.h"
31+
#include "mlir/Target/LLVMIR/Dialect/VCIX/VCIXToLLVMIRTranslation.h"
3132
#include "mlir/Target/LLVMIR/Dialect/X86Vector/X86VectorToLLVMIRTranslation.h"
3233

3334
namespace mlir {
@@ -48,6 +49,7 @@ static inline void registerAllToLLVMIRTranslations(DialectRegistry &registry) {
4849
registerOpenMPDialectTranslation(registry);
4950
registerROCDLDialectTranslation(registry);
5051
registerSPIRVDialectTranslation(registry);
52+
registerVCIXDialectTranslation(registry);
5153
registerX86VectorDialectTranslation(registry);
5254

5355
// Extension required for translating GPU offloading Ops.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- VCIXToLLVMIRTranslation.h - VCIX to LLVM IR ------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This provides registration calls for VCIX dialect to LLVM IR translation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_LLVMIR_DIALECT_VCIX_VCIXTOLLVMIRTRANSLATION_H
14+
#define MLIR_TARGET_LLVMIR_DIALECT_VCIX_VCIXTOLLVMIRTRANSLATION_H
15+
16+
namespace mlir {
17+
18+
class DialectRegistry;
19+
class MLIRContext;
20+
21+
/// Register the VCIX dialect and the translation from it to the LLVM IR in the
22+
/// given registry.
23+
void registerVCIXDialectTranslation(DialectRegistry &registry);
24+
25+
/// Register the VCIX dialect and the translation from it in the registry
26+
/// associated with the given context.
27+
void registerVCIXDialectTranslation(MLIRContext &context);
28+
29+
} // namespace mlir
30+
31+
#endif // MLIR_TARGET_LLVMIR_DIALECT_VCIX_VCIXTOLLVMIRTRANSLATION_H

mlir/lib/Dialect/LLVMIR/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,25 @@ add_mlir_dialect_library(MLIRROCDLDialect
8585
MLIRLLVMDialect
8686
MLIRSideEffectInterfaces
8787
)
88+
89+
add_mlir_dialect_library(MLIRVCIXDialect
90+
IR/VCIXDialect.cpp
91+
92+
ADDITIONAL_HEADER_DIRS
93+
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/LLVMIR
94+
95+
DEPENDS
96+
MLIRGPUCompilationAttrInterfacesIncGen
97+
MLIRVCIXOpsIncGen
98+
MLIRVCIXConversionsIncGen
99+
intrinsics_gen
100+
101+
LINK_COMPONENTS
102+
AsmParser
103+
Core
104+
105+
LINK_LIBS PUBLIC
106+
MLIRIR
107+
MLIRLLVMDialect
108+
MLIRSideEffectInterfaces
109+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===- VCIXDialect.cpp - MLIR VCIX ops implementation ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements the VCIX dialect and its operations.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "mlir/Dialect/LLVMIR/VCIXDialect.h"
14+
15+
#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
16+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
17+
#include "mlir/IR/Builders.h"
18+
#include "mlir/IR/BuiltinTypes.h"
19+
#include "mlir/IR/DialectImplementation.h"
20+
#include "mlir/IR/MLIRContext.h"
21+
#include "mlir/IR/Operation.h"
22+
#include "llvm/ADT/TypeSwitch.h"
23+
#include "llvm/AsmParser/Parser.h"
24+
#include "llvm/IR/Attributes.h"
25+
#include "llvm/IR/Function.h"
26+
#include "llvm/IR/Type.h"
27+
#include "llvm/Support/SourceMgr.h"
28+
29+
using namespace mlir;
30+
using namespace vcix;
31+
32+
#include "mlir/Dialect/LLVMIR/VCIXOpsDialect.cpp.inc"
33+
34+
//===----------------------------------------------------------------------===//
35+
// VCIXDialect initialization, type parsing, and registration.
36+
//===----------------------------------------------------------------------===//
37+
38+
void VCIXDialect::initialize() {
39+
addOperations<
40+
#define GET_OP_LIST
41+
#include "mlir/Dialect/LLVMIR/VCIXOps.cpp.inc"
42+
>();
43+
44+
addAttributes<
45+
#define GET_ATTRDEF_LIST
46+
#include "mlir/Dialect/LLVMIR/VCIXOpsAttributes.cpp.inc"
47+
>();
48+
}
49+
50+
#define GET_OP_CLASSES
51+
#include "mlir/Dialect/LLVMIR/VCIXOps.cpp.inc"
52+
53+
#define GET_ATTRDEF_CLASSES
54+
#include "mlir/Dialect/LLVMIR/VCIXOpsAttributes.cpp.inc"

mlir/lib/Target/LLVMIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
5959
MLIROpenMPToLLVMIRTranslation
6060
MLIRROCDLToLLVMIRTranslation
6161
MLIRSPIRVToLLVMIRTranslation
62+
MLIRVCIXToLLVMIRTranslation
6263
)
6364

6465
add_mlir_translation_library(MLIRTargetLLVMIRImport

mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ add_subdirectory(OpenACC)
1010
add_subdirectory(OpenMP)
1111
add_subdirectory(ROCDL)
1212
add_subdirectory(SPIRV)
13+
add_subdirectory(VCIX)
1314
add_subdirectory(X86Vector)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
add_mlir_translation_library(MLIRVCIXToLLVMIRTranslation
2+
VCIXToLLVMIRTranslation.cpp
3+
4+
DEPENDS
5+
MLIRVCIXConversionsIncGen
6+
7+
LINK_COMPONENTS
8+
Core
9+
10+
LINK_LIBS PUBLIC
11+
MLIRIR
12+
MLIRLLVMDialect
13+
MLIRVCIXDialect
14+
MLIRSupport
15+
MLIRTargetLLVMIRExport
16+
)

0 commit comments

Comments
 (0)