Skip to content

Commit d329c96

Browse files
committed
[CIR] Fix vector issues from latest rebase
1 parent a310ae0 commit d329c96

File tree

11 files changed

+67
-55
lines changed

11 files changed

+67
-55
lines changed

clang/lib/CIR/CodeGen/ABIInfo.h

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#define LLVM_CLANG_LIB_CIR_ABIINFO_H
1111

1212
#include "clang/AST/Type.h"
13+
#include "clang/Basic/LangOptions.h"
14+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1315

1416
namespace clang::CIRGen {
1517

@@ -39,6 +41,13 @@ class ABIInfo {
3941
// Implement the Type::IsPromotableIntegerType for ABI specific needs. The
4042
// only difference is that this consideres bit-precise integer types as well.
4143
bool isPromotableIntegerTypeForABI(clang::QualType Ty) const;
44+
45+
/// Returns the optimal vector memory type based on the given vector type. For
46+
/// example, on certain targets, a vector with 3 elements might be promoted to
47+
/// one with 4 elements to improve performance.
48+
virtual cir::VectorType
49+
getOptimalVectorMemoryType(cir::VectorType T,
50+
const clang::LangOptions &Opt) const;
4251
};
4352

4453
} // namespace clang::CIRGen

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

+43-49
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ static Address emitPointerWithAlignment(const Expr *expr,
173173
llvm_unreachable("NYI");
174174
}
175175

176-
auto ElemTy = cgf.convertTypeForMem(expr->getType()->getPointeeType());
176+
auto eltTy = cgf.convertTypeForMem(expr->getType()->getPointeeType());
177177
addr = cgf.getBuilder().createElementBitCast(
178-
cgf.getLoc(expr->getSourceRange()), addr, ElemTy);
178+
cgf.getLoc(expr->getSourceRange()), addr, eltTy);
179179
if (CE->getCastKind() == CK_AddressSpaceConversion) {
180180
assert(!cir::MissingFeatures::addressSpace());
181181
llvm_unreachable("NYI");
@@ -616,6 +616,25 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
616616
LValueBaseInfo baseInfo,
617617
TBAAAccessInfo tbaaInfo, bool isInit,
618618
bool isNontemporal) {
619+
assert(!cir::MissingFeatures::threadLocal() && "NYI");
620+
621+
auto eltTy = addr.getElementType();
622+
if (const auto *clangVecTy = ty->getAs<clang::VectorType>()) {
623+
// Boolean vectors use `iN` as storage type.
624+
if (clangVecTy->isExtVectorBoolType()) {
625+
llvm_unreachable("isExtVectorBoolType NYI");
626+
}
627+
628+
// Handle vectors of size 3 like size 4 for better performance.
629+
const auto vTy = cast<cir::VectorType>(eltTy);
630+
auto newVecTy =
631+
CGM.getABIInfo().getOptimalVectorMemoryType(vTy, getLangOpts());
632+
633+
if (vTy != newVecTy) {
634+
llvm_unreachable("NYI");
635+
}
636+
}
637+
619638
value = emitToMemory(value, ty);
620639

621640
LValue atomicLValue =
@@ -626,26 +645,6 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
626645
return;
627646
}
628647

629-
mlir::Type SrcTy = value.getType();
630-
if (const auto *ClangVecTy = ty->getAs<clang::VectorType>()) {
631-
// TODO(CIR): this has fallen out of date with codegen
632-
llvm_unreachable("NYI: Special treatment of 3-element vector store");
633-
// auto VecTy = dyn_cast<cir::VectorType>(SrcTy);
634-
// if (!CGM.getCodeGenOpts().PreserveVec3Type &&
635-
// ClangVecTy->getNumElements() == 3) {
636-
// // Handle vec3 special.
637-
// if (VecTy && VecTy.getSize() == 3) {
638-
// // Our source is a vec3, do a shuffle vector to make it a vec4.
639-
// value = builder.createVecShuffle(value.getLoc(), value,
640-
// ArrayRef<int64_t>{0, 1, 2, -1});
641-
// SrcTy = cir::VectorType::get(VecTy.getContext(), VecTy.getEltType(), 4);
642-
// }
643-
// if (addr.getElementType() != SrcTy) {
644-
// addr = addr.withElementType(SrcTy);
645-
// }
646-
// }
647-
}
648-
649648
// Update the alloca with more info on initialization.
650649
assert(addr.getPointer() && "expected pointer to exist");
651650
auto SrcAlloca =
@@ -2917,40 +2916,36 @@ mlir::Value CIRGenFunction::emitLoadOfScalar(Address addr, bool isVolatile,
29172916
LValueBaseInfo baseInfo,
29182917
TBAAAccessInfo tbaaInfo,
29192918
bool isNontemporal) {
2920-
// TODO(CIR): this has fallen out of sync with codegen
2921-
// Atomic operations have to be done on integral types
2922-
LValue atomicLValue =
2923-
LValue::makeAddr(addr, ty, getContext(), baseInfo, tbaaInfo);
2924-
if (ty->isAtomicType() || LValueIsSuitableForInlineAtomic(atomicLValue)) {
2925-
llvm_unreachable("NYI");
2926-
}
2919+
assert(!cir::MissingFeatures::threadLocal() && "NYI");
2920+
auto eltTy = addr.getElementType();
29272921

2928-
auto ElemTy = addr.getElementType();
2922+
if (const auto *clangVecTy = ty->getAs<clang::VectorType>()) {
2923+
// Boolean vectors use `iN` as storage type.
2924+
if (clangVecTy->isExtVectorBoolType()) {
2925+
llvm_unreachable("NYI");
2926+
}
29292927

2930-
if (const auto *ClangVecTy = ty->getAs<clang::VectorType>()) {
29312928
// Handle vectors of size 3 like size 4 for better performance.
2932-
const auto VTy = cast<cir::VectorType>(ElemTy);
2929+
const auto vTy = cast<cir::VectorType>(eltTy);
2930+
auto newVecTy =
2931+
CGM.getABIInfo().getOptimalVectorMemoryType(vTy, getLangOpts());
29332932

2934-
// TODO(CIR): this has fallen out of sync with codegen
2935-
llvm_unreachable("NYI: Special treatment of 3-element vector store");
2936-
// if (!CGM.getCodeGenOpts().PreserveVec3Type &&
2937-
// ClangVecTy->getNumElements() == 3) {
2938-
// auto loc = addr.getPointer().getLoc();
2939-
// auto vec4Ty = cir::VectorType::get(VTy.getContext(), VTy.getEltType(), 4);
2940-
// Address Cast = addr.withElementType(vec4Ty);
2941-
// // Now load value.
2942-
// mlir::Value V = builder.createLoad(loc, Cast);
2933+
if (vTy != newVecTy) {
2934+
llvm_unreachable("NYI");
2935+
}
2936+
}
29432937

2944-
// // Shuffle vector to get vec3.
2945-
// V = builder.createVecShuffle(loc, V, ArrayRef<int64_t>{0, 1, 2});
2946-
// return emitFromMemory(V, ty);
2947-
// }
2938+
LValue atomicLValue =
2939+
LValue::makeAddr(addr, ty, getContext(), baseInfo, tbaaInfo);
2940+
if (ty->isAtomicType() || LValueIsSuitableForInlineAtomic(atomicLValue)) {
2941+
llvm_unreachable("NYI");
29482942
}
29492943

2944+
// TODO(cir): modernize this with addr.withElementType(convertTypeForLoadStore
29502945
auto Ptr = addr.getPointer();
2951-
if (mlir::isa<cir::VoidType>(ElemTy)) {
2952-
ElemTy = cir::IntType::get(&getMLIRContext(), 8, true);
2953-
auto ElemPtrTy = cir::PointerType::get(&getMLIRContext(), ElemTy);
2946+
if (mlir::isa<cir::VoidType>(eltTy)) {
2947+
eltTy = cir::IntType::get(&getMLIRContext(), 8, true);
2948+
auto ElemPtrTy = cir::PointerType::get(&getMLIRContext(), eltTy);
29542949
Ptr = builder.create<cir::CastOp>(loc, ElemPtrTy, cir::CastKind::bitcast,
29552950
Ptr);
29562951
}
@@ -2962,7 +2957,6 @@ mlir::Value CIRGenFunction::emitLoadOfScalar(Address addr, bool isVolatile,
29622957
CGM.decorateOperationWithTBAA(loadOp, tbaaInfo);
29632958

29642959
assert(!cir::MissingFeatures::emitScalarRangeCheck() && "NYI");
2965-
29662960
return emitFromMemory(loadOp, ty);
29672961
}
29682962

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,10 @@ void CIRGenModule::setDSOLocal(CIRGlobalValueInterface GV) const {
494494
GV.setDSOLocal(shouldAssumeDSOLocal(*this, GV));
495495
}
496496

497+
const ABIInfo &CIRGenModule::getABIInfo() {
498+
return getTargetCIRGenInfo().getABIInfo();
499+
}
500+
497501
void CIRGenModule::emitGlobal(GlobalDecl GD) {
498502
llvm::TimeTraceScope scope("build CIR Global", [&]() -> std::string {
499503
auto *ND = dyn_cast<NamedDecl>(GD.getDecl());

clang/lib/CIR/CodeGen/CIRGenModule.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ class CIRGenModule : public CIRGenTypeCache {
471471
/// NOTE: This should only be called for definitions.
472472
void setCommonAttributes(GlobalDecl GD, mlir::Operation *GV);
473473

474-
// TODO: this obviously overlaps with
475474
const TargetCIRGenInfo &getTargetCIRGenInfo();
475+
const ABIInfo &getABIInfo();
476476

477477
/// Helpers to convert Clang's SourceLocation to a MLIR Location.
478478
mlir::Location getLoc(clang::SourceLocation SLoc);

clang/lib/CIR/CodeGen/TargetInfo.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,15 @@ static bool classifyReturnType(const CIRGenCXXABI &CXXABI,
318318

319319
CIRGenCXXABI &ABIInfo::getCXXABI() const { return CGT.getCXXABI(); }
320320

321+
cir::VectorType
322+
ABIInfo::getOptimalVectorMemoryType(cir::VectorType T,
323+
const clang::LangOptions &Opt) const {
324+
if (T.getSize() == 3 && !Opt.PreserveVec3Type) {
325+
llvm_unreachable("NYI");
326+
}
327+
return T;
328+
}
329+
321330
clang::ASTContext &ABIInfo::getContext() const { return CGT.getContext(); }
322331

323332
cir::ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,

clang/lib/CIR/Dialect/Transforms/TargetLowering/ABIInfo.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class LowerTypes;
2727

2828
/// Target specific hooks for defining how a type should be passed or returned
2929
/// from functions.
30+
/// FIXME(cir): this needs to be merged with clang/lib/CIR/CodeGen/ABIInfo.h
3031
class ABIInfo {
3132
protected:
3233
LowerTypes &LT;

clang/test/CIR/CodeGen/OpenCL/printf.cl

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// RUN: FileCheck -input-file=%t.30fp64.ll -check-prefixes=LLVM-FP64,LLVM-ALL %s
1515
// RUN: %clang_cc1 -fclangir -no-enable-noundef-analysis -cl-std=CL3.0 -cl-ext=-__opencl_c_fp64,-cl_khr_fp64 -triple spirv64-unknown-unknown -disable-llvm-passes -emit-llvm -fno-clangir-call-conv-lowering -o %t.30nofp64.ll %s
1616
// RUN: FileCheck -input-file=%t.30nofp64.ll -check-prefixes=LLVM-NOFP64,LLVM-ALL %s
17-
// XFAIL: *
1817

1918
typedef __attribute__((ext_vector_type(2))) float float2;
2019
typedef __attribute__((ext_vector_type(2))) half half2;

clang/test/CIR/CodeGen/builtins-elementwise.c

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// RUN: %clang_cc1 -triple aarch64-none-linux-android24 -fclangir \
44
// RUN: -emit-llvm %s -o %t.ll
55
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
6-
// XFAIL: *
76

87
typedef int vint4 __attribute__((ext_vector_type(4)));
98
typedef float vfloat4 __attribute__((ext_vector_type(4)));

clang/test/CIR/CodeGen/vectype.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s
2-
// XFAIL: *
32

43
typedef int vi4 __attribute__((vector_size(16)));
54
typedef double vd2 __attribute__((vector_size(16)));

clang/test/CIR/Lowering/ThroughMLIR/vectype.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fno-clangir-direct-lowering -emit-mlir %s -o %t.mlir
22
// RUN: FileCheck --input-file=%t.mlir %s
3-
// XFAIL: *
43

54
typedef int vi4 __attribute__((vector_size(16)));
65

clang/test/CIR/Lowering/vectype.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// RUN: cir-opt %t.cir -cir-to-llvm -o %t.mlir
33
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ii
44
// RUN: FileCheck --input-file=%t.mlir %s
5-
// XFAIL: *
65

76
typedef int vi4 __attribute__((vector_size(16)));
87
typedef double vd2 __attribute__((vector_size(16)));

0 commit comments

Comments
 (0)