Skip to content

Commit 3e54155

Browse files
committed
Move getZeroInitAttr to CIRBaseBuilder
1 parent a1c48ae commit 3e54155

File tree

4 files changed

+49
-48
lines changed

4 files changed

+49
-48
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

+29
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
4141
public:
4242
CIRBaseBuilderTy(mlir::MLIRContext &C) : mlir::OpBuilder(&C) {}
4343

44+
mlir::cir::BoolType getBoolTy() {
45+
return ::mlir::cir::BoolType::get(getContext());
46+
}
47+
48+
mlir::cir::BoolAttr getCIRBoolAttr(bool state) {
49+
return mlir::cir::BoolAttr::get(getContext(), getBoolTy(), state);
50+
}
51+
52+
mlir::TypedAttr getZeroInitAttr(mlir::Type ty) {
53+
if (ty.isa<mlir::cir::IntType>())
54+
return mlir::cir::IntAttr::get(ty, 0);
55+
if (auto fltType = ty.dyn_cast<mlir::cir::SingleType>())
56+
return mlir::cir::FPAttr::getZero(fltType);
57+
if (auto fltType = ty.dyn_cast<mlir::cir::DoubleType>())
58+
return mlir::cir::FPAttr::getZero(fltType);
59+
if (auto complexTy = ty.dyn_cast<mlir::cir::ComplexType>())
60+
return mlir::cir::ComplexAttr::getZero(complexTy);
61+
if (auto arrTy = ty.dyn_cast<mlir::cir::ArrayType>())
62+
return getZeroAttr(arrTy);
63+
if (auto ptrTy = ty.dyn_cast<mlir::cir::PointerType>())
64+
return getConstPtrAttr(ptrTy, 0);
65+
if (auto structTy = ty.dyn_cast<mlir::cir::StructType>())
66+
return getZeroAttr(structTy);
67+
if (ty.isa<mlir::cir::BoolType>()) {
68+
return getCIRBoolAttr(false);
69+
}
70+
llvm_unreachable("Zero initializer for given type is NYI");
71+
}
72+
4473
mlir::Value getConstAPSInt(mlir::Location loc, const llvm::APSInt &val) {
4574
auto ty = mlir::cir::IntType::get(getContext(), val.getBitWidth(),
4675
val.isSigned());

clang/lib/CIR/CodeGen/CIRGenBuilder.h

-28
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,6 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
140140
return mlir::cir::ZeroAttr::get(getContext(), t);
141141
}
142142

143-
mlir::cir::BoolAttr getCIRBoolAttr(bool state) {
144-
return mlir::cir::BoolAttr::get(getContext(), getBoolTy(), state);
145-
}
146-
147143
mlir::TypedAttr getConstNullPtrAttr(mlir::Type t) {
148144
assert(t.isa<mlir::cir::PointerType>() && "expected cir.ptr");
149145
return mlir::cir::ConstPtrAttr::get(getContext(), t, 0);
@@ -231,27 +227,6 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
231227
return mlir::cir::DataMemberAttr::get(getContext(), ty, std::nullopt);
232228
}
233229

234-
mlir::TypedAttr getZeroInitAttr(mlir::Type ty) {
235-
if (ty.isa<mlir::cir::IntType>())
236-
return mlir::cir::IntAttr::get(ty, 0);
237-
if (auto fltType = ty.dyn_cast<mlir::cir::SingleType>())
238-
return mlir::cir::FPAttr::getZero(fltType);
239-
if (auto fltType = ty.dyn_cast<mlir::cir::DoubleType>())
240-
return mlir::cir::FPAttr::getZero(fltType);
241-
if (auto complexTy = ty.dyn_cast<mlir::cir::ComplexType>())
242-
return mlir::cir::ComplexAttr::getZero(complexTy);
243-
if (auto arrTy = ty.dyn_cast<mlir::cir::ArrayType>())
244-
return getZeroAttr(arrTy);
245-
if (auto ptrTy = ty.dyn_cast<mlir::cir::PointerType>())
246-
return getConstPtrAttr(ptrTy, 0);
247-
if (auto structTy = ty.dyn_cast<mlir::cir::StructType>())
248-
return getZeroAttr(structTy);
249-
if (ty.isa<mlir::cir::BoolType>()) {
250-
return getCIRBoolAttr(false);
251-
}
252-
llvm_unreachable("Zero initializer for given type is NYI");
253-
}
254-
255230
// TODO(cir): Once we have CIR float types, replace this by something like a
256231
// NullableValueInterface to allow for type-independent queries.
257232
bool isNullValue(mlir::Attribute attr) const {
@@ -388,9 +363,6 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
388363
llvm_unreachable("Unknown float format!");
389364
}
390365

391-
mlir::cir::BoolType getBoolTy() {
392-
return ::mlir::cir::BoolType::get(getContext());
393-
}
394366
mlir::Type getVirtualFnPtrType(bool isVarArg = false) {
395367
// FIXME: replay LLVM codegen for now, perhaps add a vtable ptr special
396368
// type so it's a bit more clear and C++ idiomatic.

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

+16-20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
14+
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
1415
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1516
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
1617
#include "clang/CIR/Dialect/IR/CIRTypes.h"
@@ -322,7 +323,7 @@ parseFloatLiteral(mlir::AsmParser &parser,
322323
auto losesInfo = false;
323324
value.emplace(rawValue);
324325

325-
auto tyFpInterface = ty.dyn_cast<cir::CIRFPTypeInterface>();
326+
auto tyFpInterface = ty.dyn_cast<mlir::cir::CIRFPTypeInterface>();
326327
if (!tyFpInterface) {
327328
// Parsing of the current floating-point literal has succeeded, but the
328329
// given attribute type is invalid. This error will be reported later when
@@ -335,14 +336,16 @@ parseFloatLiteral(mlir::AsmParser &parser,
335336
return success();
336337
}
337338

338-
cir::FPAttr cir::FPAttr::getZero(mlir::Type type) {
339-
return get(type,
340-
APFloat::getZero(
341-
type.cast<cir::CIRFPTypeInterface>().getFloatSemantics()));
339+
mlir::cir::FPAttr mlir::cir::FPAttr::getZero(mlir::Type type) {
340+
return get(
341+
type,
342+
APFloat::getZero(
343+
type.cast<mlir::cir::CIRFPTypeInterface>().getFloatSemantics()));
342344
}
343345

344-
LogicalResult cir::FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
345-
Type type, APFloat value) {
346+
LogicalResult
347+
mlir::cir::FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
348+
Type type, APFloat value) {
346349
auto fltTypeInterface = type.dyn_cast<cir::CIRFPTypeInterface>();
347350
if (!fltTypeInterface) {
348351
emitError() << "expected floating-point type";
@@ -362,8 +365,8 @@ LogicalResult cir::FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
362365
//===----------------------------------------------------------------------===//
363366

364367
LogicalResult
365-
cir::ComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
366-
Type type, TypedAttr real, TypedAttr imag) {
368+
mlir::cir::ComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
369+
Type type, TypedAttr real, TypedAttr imag) {
367370
auto complexTy = type.dyn_cast<mlir::cir::ComplexType>();
368371
if (!complexTy) {
369372
emitError() << "expected complex type";
@@ -383,21 +386,14 @@ cir::ComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
383386
return success();
384387
}
385388

386-
cir::ComplexAttr cir::ComplexAttr::getZero(Type type) {
389+
mlir::cir::ComplexAttr mlir::cir::ComplexAttr::getZero(Type type) {
387390
auto complexTy = type.cast<mlir::cir::ComplexType>();
388391
auto elementTy = complexTy.getElementTy();
389392

390-
mlir::TypedAttr real;
391-
mlir::TypedAttr imag;
392-
393-
if (elementTy.isa<mlir::cir::IntType>()) {
394-
real = mlir::cir::IntAttr::get(elementTy, 0);
395-
imag = mlir::cir::IntAttr::get(elementTy, 0);
396-
} else {
397-
real = mlir::cir::FPAttr::getZero(elementTy);
398-
imag = mlir::cir::FPAttr::getZero(elementTy);
399-
}
393+
::cir::CIRBaseBuilderTy builder(*type.getContext());
400394

395+
auto real = builder.getZeroInitAttr(elementTy);
396+
auto imag = builder.getZeroInitAttr(elementTy);
401397
return get(type, real, imag);
402398
}
403399

clang/test/CIR/CodeGen/complex.c

+4
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,7 @@ float _Complex bin_op_with_real(float x, float _Complex y) {
9090
// CHECK: %[[#OP:]] = cir.cast(float_to_complex, %{{.+}} : !cir.float), !cir.complex<!cir.float>
9191
// CHECK-NEXT: %{{.+}} = cir.binop(add, %[[#OP]], %{{.+}}) : !cir.complex<!cir.float>
9292
// CHECK: }
93+
94+
double _Complex global;
95+
96+
// CHECK: cir.global external @global = #cir.complex<#cir.fp<0.000000e+00> : !cir.double, #cir.fp<0.000000e+00> : !cir.double> : !cir.complex<!cir.double>

0 commit comments

Comments
 (0)