https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/136488
>From 5d07a639dc6c933f6f171e501e4ec20d731553e3 Mon Sep 17 00:00:00 2001 From: AmrDeveloper <am...@programmer.net> Date: Sat, 19 Apr 2025 21:03:07 +0200 Subject: [PATCH] [CIR] Upstream initial support for fixed size VectorType --- .../CIR/Dialect/Builder/CIRBaseBuilder.h | 2 + .../include/clang/CIR/Dialect/IR/CIRTypes.td | 22 +++++- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 3 + clang/lib/CIR/CodeGen/CIRGenTypes.cpp | 9 +++ clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 2 +- clang/lib/CIR/Dialect/IR/CIRTypes.cpp | 19 ++++- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 5 ++ clang/test/CIR/CodeGen/vector-ext.cpp | 73 +++++++++++++++++++ clang/test/CIR/CodeGen/vector.cpp | 60 +++++++++++++++ clang/test/CIR/IR/vector.cir | 40 ++++++++++ 10 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 clang/test/CIR/CodeGen/vector-ext.cpp create mode 100644 clang/test/CIR/CodeGen/vector.cpp create mode 100644 clang/test/CIR/IR/vector.cir diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h index d2a241964f34f..04d9fec9f3001 100644 --- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h +++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h @@ -93,6 +93,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { return cir::FPAttr::getZero(ty); if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty)) return getZeroAttr(arrTy); + if (auto vecTy = mlir::dyn_cast<cir::VectorType>(ty)) + return getZeroAttr(vecTy); if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty)) return getConstNullPtrAttr(ptrTy); if (auto recordTy = mlir::dyn_cast<cir::RecordType>(ty)) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index a552b6081f5dc..8fb46cea618a3 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -307,6 +307,26 @@ def CIR_ArrayType : CIR_Type<"Array", "array", }]; } +//===----------------------------------------------------------------------===// +// VectorType (fixed size) +//===----------------------------------------------------------------------===// + +def CIR_VectorType : CIR_Type<"Vector", "vector", + [DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> { + + let summary = "CIR vector type"; + let description = [{ + `cir.vector' represents fixed-size vector types. The parameters are the + element type and the number of elements. + }]; + + let parameters = (ins "mlir::Type":$eltType, "uint64_t":$size); + + let assemblyFormat = [{ + `<` $eltType `x` $size `>` + }]; +} + //===----------------------------------------------------------------------===// // FuncType //===----------------------------------------------------------------------===// @@ -530,7 +550,7 @@ def CIRRecordType : Type< //===----------------------------------------------------------------------===// def CIR_AnyType : AnyTypeOf<[ - CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_IntType, CIR_AnyFloat, + CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_VectorType, CIR_IntType, CIR_AnyFloat, CIR_PointerType, CIR_FuncType, CIR_RecordType ]>; diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index d98416734b56e..ae4f5c5ebef94 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -82,6 +82,9 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { cir::IntType>(ty)) return true; + if (mlir::isa<cir::VectorType>(ty)) + return isSized(mlir::cast<cir::VectorType>(ty).getEltType()); + assert(!cir::MissingFeatures::unsizedTypes()); return false; } diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp index c286aef360b01..01e8f3576fbe3 100644 --- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp @@ -399,6 +399,15 @@ mlir::Type CIRGenTypes::convertType(QualType type) { break; } + case Type::ExtVector: + case Type::Vector: { + const VectorType *vec = cast<VectorType>(ty); + const mlir::Type elemTy = convertTypeForMem(vec->getElementType()); + resultType = cir::VectorType::get(builder.getContext(), elemTy, + vec->getNumElements()); + break; + } + case Type::FunctionNoProto: case Type::FunctionProto: resultType = convertFunctionTypeInternal(type); diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index d2313e75870b4..1011f1b76b19b 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -212,7 +212,7 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType, } if (isa<cir::ZeroAttr>(attrType)) { - if (isa<cir::RecordType, cir::ArrayType>(opType)) + if (isa<cir::RecordType, cir::ArrayType, cir::VectorType>(opType)) return success(); return op->emitOpError("zero expects struct or array type"); } diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp index 8b5646f339bb3..f4ec524b65129 100644 --- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp @@ -622,7 +622,7 @@ BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout, } //===----------------------------------------------------------------------===// -// Definitions +// ArrayType Definitions //===----------------------------------------------------------------------===// llvm::TypeSize @@ -637,6 +637,23 @@ ArrayType::getABIAlignment(const ::mlir::DataLayout &dataLayout, return dataLayout.getTypeABIAlignment(getEltType()); } +//===----------------------------------------------------------------------===// +// VectorType Definitions +//===----------------------------------------------------------------------===// + +llvm::TypeSize cir::VectorType::getTypeSizeInBits( + const ::mlir::DataLayout &dataLayout, + ::mlir::DataLayoutEntryListRef params) const { + return llvm::TypeSize::getFixed(getSize() * + dataLayout.getTypeSizeInBits(getEltType())); +} + +uint64_t +cir::VectorType::getABIAlignment(const ::mlir::DataLayout &dataLayout, + ::mlir::DataLayoutEntryListRef params) const { + return llvm::NextPowerOf2(dataLayout.getTypeSizeInBits(*this)); +} + //===----------------------------------------------------------------------===// // PointerType Definitions //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 8c4a67258df3f..9c35327e43215 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -19,6 +19,7 @@ #include "mlir/Dialect/DLTI/DLTI.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "mlir/Dialect/LLVMIR/LLVMTypes.h" #include "mlir/IR/BuiltinDialect.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/IR/Types.h" @@ -1308,6 +1309,10 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter, convertTypeForMemory(converter, dataLayout, type.getEltType()); return mlir::LLVM::LLVMArrayType::get(ty, type.getSize()); }); + converter.addConversion([&](cir::VectorType type) -> mlir::Type { + const mlir::Type ty = converter.convertType(type.getEltType()); + return mlir::VectorType::get(type.getSize(), ty); + }); converter.addConversion([&](cir::BoolType type) -> mlir::Type { return mlir::IntegerType::get(type.getContext(), 1, mlir::IntegerType::Signless); diff --git a/clang/test/CIR/CodeGen/vector-ext.cpp b/clang/test/CIR/CodeGen/vector-ext.cpp new file mode 100644 index 0000000000000..9c3fdb0b9c31b --- /dev/null +++ b/clang/test/CIR/CodeGen/vector-ext.cpp @@ -0,0 +1,73 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG + +typedef int vi4 __attribute__((ext_vector_type(4))); +typedef int vi3 __attribute__((ext_vector_type(3))); +typedef int vi2 __attribute__((ext_vector_type(2))); +typedef double vd2 __attribute__((ext_vector_type(2))); + +vi4 vec_a; +// CIR: cir.global external @[[VEC_A:.*]] = #cir.zero : !cir.vector<!s32i x 4> + +// LLVM: @[[VEC_A:.*]] = dso_local global <4 x i32> zeroinitializer + +// OGCG: @[[VEC_A:.*]] = global <4 x i32> zeroinitializer + +vi3 vec_b; +// CIR: cir.global external @[[VEC_B:.*]] = #cir.zero : !cir.vector<!s32i x 3> + +// LLVM: @[[VEC_B:.*]] = dso_local global <3 x i32> zeroinitializer + +// OGCG: @[[VEC_B:.*]] = global <3 x i32> zeroinitializer + +vi2 vec_c; +// CIR: cir.global external @[[VEC_C:.*]] = #cir.zero : !cir.vector<!s32i x 2> + +// LLVM: @[[VEC_C:.*]] = dso_local global <2 x i32> zeroinitializer + +// OGCG: @[[VEC_C:.*]] = global <2 x i32> zeroinitializer + +vd2 d; + +// CIR: cir.global external @[[VEC_D:.*]] = #cir.zero : !cir.vector<!cir.double x 2> + +// LLVM: @[[VEC_D:.*]] = dso_local global <2 x double> zeroinitialize + +// OGCG: @[[VEC_D:.*]] = global <2 x double> zeroinitializer + +void foo() { + vi4 a; + vi3 b; + vi2 c; + vd2 d; +} + +// CIR: %[[VEC_A:.*]] = cir.alloca !cir.vector<!s32i x 4>, !cir.ptr<!cir.vector<!s32i x 4>>, ["a"] +// CIR: %[[VEC_B:.*]] = cir.alloca !cir.vector<!s32i x 3>, !cir.ptr<!cir.vector<!s32i x 3>>, ["b"] +// CIR: %[[VEC_C:.*]] = cir.alloca !cir.vector<!s32i x 2>, !cir.ptr<!cir.vector<!s32i x 2>>, ["c"] +// CIR: %[[VEC_D:.*]] = cir.alloca !cir.vector<!cir.double x 2>, !cir.ptr<!cir.vector<!cir.double x 2>>, ["d"] + +// LLVM: %[[VEC_A:.*]] = alloca <4 x i32>, i64 1, align 16 +// LLVM: %[[VEC_B:.*]] = alloca <3 x i32>, i64 1, align 16 +// LLVM: %[[VEC_C:.*]] = alloca <2 x i32>, i64 1, align 8 +// LLVM: %[[VEC_D:.*]] = alloca <2 x double>, i64 1, align 16 + +// OGCG: %[[VEC_A:.*]] = alloca <4 x i32>, align 16 +// OGCG: %[[VEC_B:.*]] = alloca <3 x i32>, align 16 +// OGCG: %[[VEC_C:.*]] = alloca <2 x i32>, align 8 +// OGCG: %[[VEC_D:.*]] = alloca <2 x double>, align 16 + +void foo2(vi4 p) {} + +// CIR: %[[VEC_A:.*]] = cir.alloca !cir.vector<!s32i x 4>, !cir.ptr<!cir.vector<!s32i x 4>>, ["p", init] +// CIR: cir.store %{{.*}}, %[[VEC_A]] : !cir.vector<!s32i x 4>, !cir.ptr<!cir.vector<!s32i x 4>> + +// LLVM: %[[VEC_A:.*]] = alloca <4 x i32>, i64 1, align 16 +// LLVM: store <4 x i32> %{{.*}}, ptr %[[VEC_A]], align 16 + +// OGCG: %[[VEC_A:.*]] = alloca <4 x i32>, align 16 +// OGCG: store <4 x i32> %{{.*}}, ptr %[[VEC_A]], align 16 diff --git a/clang/test/CIR/CodeGen/vector.cpp b/clang/test/CIR/CodeGen/vector.cpp new file mode 100644 index 0000000000000..155a00667e9b9 --- /dev/null +++ b/clang/test/CIR/CodeGen/vector.cpp @@ -0,0 +1,60 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG + +typedef int vi4 __attribute__((vector_size(16))); +typedef double vd2 __attribute__((vector_size(16))); +typedef long long vll2 __attribute__((vector_size(16))); + +vi4 vec_a; +// CIR: cir.global external @[[VEC_A:.*]] = #cir.zero : !cir.vector<!s32i x 4> + +// LLVM: @[[VEC_A:.*]] = dso_local global <4 x i32> zeroinitializer + +// OGCG: @[[VEC_A:.*]] = global <4 x i32> zeroinitializer + +vd2 b; +// CIR: cir.global external @[[VEC_B:.*]] = #cir.zero : !cir.vector<!cir.double x 2> + +// LLVM: @[[VEC_B:.*]] = dso_local global <2 x double> zeroinitialize + +// OGCG: @[[VEC_B:.*]] = global <2 x double> zeroinitializer + +vll2 c; +// CIR: cir.global external @[[VEC_C:.*]] = #cir.zero : !cir.vector<!s64i x 2> + +// LLVM: @[[VEC_C:.*]] = dso_local global <2 x i64> zeroinitialize + +// OGCG: @[[VEC_C:.*]] = global <2 x i64> zeroinitializer + +void vec_int_test() { + vi4 a; + vd2 b; + vll2 c; +} + +// CIR: %[[VEC_A:.*]] = cir.alloca !cir.vector<!s32i x 4>, !cir.ptr<!cir.vector<!s32i x 4>>, ["a"] +// CIR: %[[VEC_B:.*]] = cir.alloca !cir.vector<!cir.double x 2>, !cir.ptr<!cir.vector<!cir.double x 2>>, ["b"] +// CIR: %[[VEC_C:.*]] = cir.alloca !cir.vector<!s64i x 2>, !cir.ptr<!cir.vector<!s64i x 2>>, ["c"] + +// LLVM: %[[VEC_A:.*]] = alloca <4 x i32>, i64 1, align 16 +// LLVM: %[[VEC_B:.*]] = alloca <2 x double>, i64 1, align 16 +// LLVM: %[[VEC_C:.*]] = alloca <2 x i64>, i64 1, align 16 + +// OGCG: %[[VEC_A:.*]] = alloca <4 x i32>, align 16 +// OGCG: %[[VEC_B:.*]] = alloca <2 x double>, align 16 +// OGCG: %[[VEC_C:.*]] = alloca <2 x i64>, align 16 + +void foo2(vi4 p) {} + +// CIR: %[[VEC_A:.*]] = cir.alloca !cir.vector<!s32i x 4>, !cir.ptr<!cir.vector<!s32i x 4>>, ["p", init] +// CIR: cir.store %{{.*}}, %[[VEC_A]] : !cir.vector<!s32i x 4>, !cir.ptr<!cir.vector<!s32i x 4>> + +// LLVM: %[[VEC_A:.*]] = alloca <4 x i32>, i64 1, align 16 +// LLVM: store <4 x i32> %{{.*}}, ptr %[[VEC_A]], align 16 + +// OGCG: %[[VEC_A:.*]] = alloca <4 x i32>, align 16 +// OGCG: store <4 x i32> %{{.*}}, ptr %[[VEC_A]], align 16 diff --git a/clang/test/CIR/IR/vector.cir b/clang/test/CIR/IR/vector.cir new file mode 100644 index 0000000000000..6eb0a269ad5ef --- /dev/null +++ b/clang/test/CIR/IR/vector.cir @@ -0,0 +1,40 @@ +// RUN: cir-opt %s | FileCheck %s + +!s32i = !cir.int<s, 32> + +module { + +cir.global external @vec_a = #cir.zero : !cir.vector<!s32i x 4> +// CHECK: cir.global external @vec_a = #cir.zero : !cir.vector<!s32i x 4> + +cir.global external @vec_b = #cir.zero : !cir.vector<!s32i x 3> +// CHECK: cir.global external @vec_b = #cir.zero : !cir.vector<!s32i x 3> + +cir.global external @vec_c = #cir.zero : !cir.vector<!s32i x 2> +// CHECK: cir.global external @vec_c = #cir.zero : !cir.vector<!s32i x 2> + +cir.func @vec_int_test() { + %0 = cir.alloca !cir.vector<!s32i x 4>, !cir.ptr<!cir.vector<!s32i x 4>>, ["a"] + %1 = cir.alloca !cir.vector<!s32i x 3>, !cir.ptr<!cir.vector<!s32i x 3>>, ["b"] + %2 = cir.alloca !cir.vector<!s32i x 2>, !cir.ptr<!cir.vector<!s32i x 2>>, ["c"] + cir.return +} + +// CHECK: cir.func @vec_int_test() { +// CHECK: %0 = cir.alloca !cir.vector<!s32i x 4>, !cir.ptr<!cir.vector<!s32i x 4>>, ["a"] +// CHECK: %1 = cir.alloca !cir.vector<!s32i x 3>, !cir.ptr<!cir.vector<!s32i x 3>>, ["b"] +// CHECK: %2 = cir.alloca !cir.vector<!s32i x 2>, !cir.ptr<!cir.vector<!s32i x 2>>, ["c"] +// CHECK: cir.return +// CHECK: } + +cir.func @vec_double_test() { + %0 = cir.alloca !cir.vector<!cir.double x 2>, !cir.ptr<!cir.vector<!cir.double x 2>>, ["a"] + cir.return +} + +// CHECK: cir.func @vec_double_test() { +// CHECK: %0 = cir.alloca !cir.vector<!cir.double x 2>, !cir.ptr<!cir.vector<!cir.double x 2>>, ["a"] +// CHECK: cir.return +// CHECK: } + +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits