Skip to content

Commit dde0549

Browse files
authored
[CIR][NFC] Replace CIR attr visitor base class with a type switch (#1330)
This change refactors the CirAttrVisitor use to eliminate the base class and instead use an llvm::TypeSwitch in the derived class to visit CIR attributes. No observable change is intended.
1 parent 2c7e65e commit dde0549

File tree

5 files changed

+38
-112
lines changed

5 files changed

+38
-112
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrVisitor.h

Lines changed: 0 additions & 47 deletions
This file was deleted.

clang/include/clang/CIR/Dialect/IR/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ mlir_tablegen(CIROpsStructs.h.inc -gen-attrdef-decls)
2626
mlir_tablegen(CIROpsStructs.cpp.inc -gen-attrdef-defs)
2727
mlir_tablegen(CIROpsAttributes.h.inc -gen-attrdef-decls)
2828
mlir_tablegen(CIROpsAttributes.cpp.inc -gen-attrdef-defs)
29-
mlir_tablegen(CIRAttrDefsList.inc -gen-attrdef-list)
3029
add_public_tablegen_target(MLIRCIREnumsGen)
3130

3231
clang_tablegen(CIRBuiltinsLowering.inc -gen-cir-builtins-lowering

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
4242
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
4343
#include "mlir/Target/LLVMIR/Export.h"
44-
#include "clang/CIR/Dialect/IR/CIRAttrVisitor.h"
4544
#include "clang/CIR/Dialect/Passes.h"
4645
#include "clang/CIR/LoweringHelpers.h"
4746
#include "clang/CIR/MissingFeatures.h"
@@ -52,6 +51,7 @@
5251
#include "llvm/ADT/SmallVector.h"
5352
#include "llvm/ADT/StringRef.h"
5453
#include "llvm/ADT/Twine.h"
54+
#include "llvm/ADT/TypeSwitch.h"
5555
#include "llvm/IR/DataLayout.h"
5656
#include "llvm/IR/DerivedTypes.h"
5757
#include "llvm/Support/Casting.h"
@@ -426,7 +426,7 @@ emitCirAttrToMemory(mlir::Operation *parentOp, mlir::Attribute attr,
426426
}
427427

428428
/// Switches on the type of attribute and calls the appropriate conversion.
429-
class CirAttrToValue : public CirAttrVisitor<CirAttrToValue, mlir::Value> {
429+
class CirAttrToValue {
430430
public:
431431
CirAttrToValue(mlir::Operation *parentOp,
432432
mlir::ConversionPatternRewriter &rewriter,
@@ -435,19 +435,29 @@ class CirAttrToValue : public CirAttrVisitor<CirAttrToValue, mlir::Value> {
435435
: parentOp(parentOp), rewriter(rewriter), converter(converter),
436436
dataLayout(dataLayout) {}
437437

438-
mlir::Value visitCirIntAttr(cir::IntAttr attr);
439-
mlir::Value visitCirFPAttr(cir::FPAttr attr);
440-
mlir::Value visitCirConstPtrAttr(cir::ConstPtrAttr attr);
441-
mlir::Value visitCirConstStructAttr(cir::ConstStructAttr attr);
442-
mlir::Value visitCirConstArrayAttr(cir::ConstArrayAttr attr);
443-
mlir::Value visitCirConstVectorAttr(cir::ConstVectorAttr attr);
444-
mlir::Value visitCirBoolAttr(cir::BoolAttr attr);
445-
mlir::Value visitCirZeroAttr(cir::ZeroAttr attr);
446-
mlir::Value visitCirUndefAttr(cir::UndefAttr attr);
447-
mlir::Value visitCirPoisonAttr(cir::PoisonAttr attr);
448-
mlir::Value visitCirGlobalViewAttr(cir::GlobalViewAttr attr);
449-
mlir::Value visitCirVTableAttr(cir::VTableAttr attr);
450-
mlir::Value visitCirTypeInfoAttr(cir::TypeInfoAttr attr);
438+
mlir::Value visit(mlir::Attribute attr) {
439+
return llvm::TypeSwitch<mlir::Attribute, mlir::Value>(attr)
440+
.Case<cir::IntAttr, cir::FPAttr, cir::ConstPtrAttr,
441+
cir::ConstStructAttr, cir::ConstArrayAttr, cir::ConstVectorAttr,
442+
cir::BoolAttr, cir::ZeroAttr, cir::UndefAttr, cir::PoisonAttr,
443+
cir::GlobalViewAttr, cir::VTableAttr, cir::TypeInfoAttr>(
444+
[&](auto attrT) { return visitCirAttr(attrT); })
445+
.Default([&](auto attrT) { return mlir::Value(); });
446+
}
447+
448+
mlir::Value visitCirAttr(cir::IntAttr attr);
449+
mlir::Value visitCirAttr(cir::FPAttr attr);
450+
mlir::Value visitCirAttr(cir::ConstPtrAttr attr);
451+
mlir::Value visitCirAttr(cir::ConstStructAttr attr);
452+
mlir::Value visitCirAttr(cir::ConstArrayAttr attr);
453+
mlir::Value visitCirAttr(cir::ConstVectorAttr attr);
454+
mlir::Value visitCirAttr(cir::BoolAttr attr);
455+
mlir::Value visitCirAttr(cir::ZeroAttr attr);
456+
mlir::Value visitCirAttr(cir::UndefAttr attr);
457+
mlir::Value visitCirAttr(cir::PoisonAttr attr);
458+
mlir::Value visitCirAttr(cir::GlobalViewAttr attr);
459+
mlir::Value visitCirAttr(cir::VTableAttr attr);
460+
mlir::Value visitCirAttr(cir::TypeInfoAttr attr);
451461

452462
private:
453463
mlir::Operation *parentOp;
@@ -457,21 +467,21 @@ class CirAttrToValue : public CirAttrVisitor<CirAttrToValue, mlir::Value> {
457467
};
458468

459469
/// IntAttr visitor.
460-
mlir::Value CirAttrToValue::visitCirIntAttr(cir::IntAttr intAttr) {
470+
mlir::Value CirAttrToValue::visitCirAttr(cir::IntAttr intAttr) {
461471
auto loc = parentOp->getLoc();
462472
return rewriter.create<mlir::LLVM::ConstantOp>(
463473
loc, converter->convertType(intAttr.getType()), intAttr.getValue());
464474
}
465475

466476
/// BoolAttr visitor.
467-
mlir::Value CirAttrToValue::visitCirBoolAttr(cir::BoolAttr boolAttr) {
477+
mlir::Value CirAttrToValue::visitCirAttr(cir::BoolAttr boolAttr) {
468478
auto loc = parentOp->getLoc();
469479
return rewriter.create<mlir::LLVM::ConstantOp>(
470480
loc, converter->convertType(boolAttr.getType()), boolAttr.getValue());
471481
}
472482

473483
/// ConstPtrAttr visitor.
474-
mlir::Value CirAttrToValue::visitCirConstPtrAttr(cir::ConstPtrAttr ptrAttr) {
484+
mlir::Value CirAttrToValue::visitCirAttr(cir::ConstPtrAttr ptrAttr) {
475485
auto loc = parentOp->getLoc();
476486
if (ptrAttr.isNullValue()) {
477487
return rewriter.create<mlir::LLVM::ZeroOp>(
@@ -486,36 +496,35 @@ mlir::Value CirAttrToValue::visitCirConstPtrAttr(cir::ConstPtrAttr ptrAttr) {
486496
}
487497

488498
/// FPAttr visitor.
489-
mlir::Value CirAttrToValue::visitCirFPAttr(cir::FPAttr fltAttr) {
499+
mlir::Value CirAttrToValue::visitCirAttr(cir::FPAttr fltAttr) {
490500
auto loc = parentOp->getLoc();
491501
return rewriter.create<mlir::LLVM::ConstantOp>(
492502
loc, converter->convertType(fltAttr.getType()), fltAttr.getValue());
493503
}
494504

495505
/// ZeroAttr visitor.
496-
mlir::Value CirAttrToValue::visitCirZeroAttr(cir::ZeroAttr zeroAttr) {
506+
mlir::Value CirAttrToValue::visitCirAttr(cir::ZeroAttr zeroAttr) {
497507
auto loc = parentOp->getLoc();
498508
return rewriter.create<mlir::LLVM::ZeroOp>(
499509
loc, converter->convertType(zeroAttr.getType()));
500510
}
501511

502512
/// UndefAttr visitor.
503-
mlir::Value CirAttrToValue::visitCirUndefAttr(cir::UndefAttr undefAttr) {
513+
mlir::Value CirAttrToValue::visitCirAttr(cir::UndefAttr undefAttr) {
504514
auto loc = parentOp->getLoc();
505515
return rewriter.create<mlir::LLVM::UndefOp>(
506516
loc, converter->convertType(undefAttr.getType()));
507517
}
508518

509519
/// PoisonAttr visitor.
510-
mlir::Value CirAttrToValue::visitCirPoisonAttr(cir::PoisonAttr poisonAttr) {
520+
mlir::Value CirAttrToValue::visitCirAttr(cir::PoisonAttr poisonAttr) {
511521
auto loc = parentOp->getLoc();
512522
return rewriter.create<mlir::LLVM::PoisonOp>(
513523
loc, converter->convertType(poisonAttr.getType()));
514524
}
515525

516526
/// ConstStruct visitor.
517-
mlir::Value
518-
CirAttrToValue::visitCirConstStructAttr(cir::ConstStructAttr constStruct) {
527+
mlir::Value CirAttrToValue::visitCirAttr(cir::ConstStructAttr constStruct) {
519528
auto llvmTy = converter->convertType(constStruct.getType());
520529
auto loc = parentOp->getLoc();
521530
mlir::Value result = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmTy);
@@ -531,7 +540,7 @@ CirAttrToValue::visitCirConstStructAttr(cir::ConstStructAttr constStruct) {
531540
}
532541

533542
// VTableAttr visitor.
534-
mlir::Value CirAttrToValue::visitCirVTableAttr(cir::VTableAttr vtableArr) {
543+
mlir::Value CirAttrToValue::visitCirAttr(cir::VTableAttr vtableArr) {
535544
auto llvmTy = converter->convertType(vtableArr.getType());
536545
auto loc = parentOp->getLoc();
537546
mlir::Value result = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmTy);
@@ -545,8 +554,7 @@ mlir::Value CirAttrToValue::visitCirVTableAttr(cir::VTableAttr vtableArr) {
545554
}
546555

547556
// TypeInfoAttr visitor.
548-
mlir::Value
549-
CirAttrToValue::visitCirTypeInfoAttr(cir::TypeInfoAttr typeinfoArr) {
557+
mlir::Value CirAttrToValue::visitCirAttr(cir::TypeInfoAttr typeinfoArr) {
550558
auto llvmTy = converter->convertType(typeinfoArr.getType());
551559
auto loc = parentOp->getLoc();
552560
mlir::Value result = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmTy);
@@ -560,8 +568,7 @@ CirAttrToValue::visitCirTypeInfoAttr(cir::TypeInfoAttr typeinfoArr) {
560568
}
561569

562570
// ConstArrayAttr visitor
563-
mlir::Value
564-
CirAttrToValue::visitCirConstArrayAttr(cir::ConstArrayAttr constArr) {
571+
mlir::Value CirAttrToValue::visitCirAttr(cir::ConstArrayAttr constArr) {
565572
auto llvmTy = converter->convertType(constArr.getType());
566573
auto loc = parentOp->getLoc();
567574
mlir::Value result;
@@ -604,8 +611,7 @@ CirAttrToValue::visitCirConstArrayAttr(cir::ConstArrayAttr constArr) {
604611
}
605612

606613
// ConstVectorAttr visitor.
607-
mlir::Value
608-
CirAttrToValue::visitCirConstVectorAttr(cir::ConstVectorAttr constVec) {
614+
mlir::Value CirAttrToValue::visitCirAttr(cir::ConstVectorAttr constVec) {
609615
auto llvmTy = converter->convertType(constVec.getType());
610616
auto loc = parentOp->getLoc();
611617
SmallVector<mlir::Attribute> mlirValues;
@@ -630,8 +636,7 @@ CirAttrToValue::visitCirConstVectorAttr(cir::ConstVectorAttr constVec) {
630636
}
631637

632638
// GlobalViewAttr visitor.
633-
mlir::Value
634-
CirAttrToValue::visitCirGlobalViewAttr(cir::GlobalViewAttr globalAttr) {
639+
mlir::Value CirAttrToValue::visitCirAttr(cir::GlobalViewAttr globalAttr) {
635640
auto module = parentOp->getParentOfType<mlir::ModuleOp>();
636641
mlir::Type sourceType;
637642
unsigned sourceAddrSpace = 0;

mlir/test/mlir-tblgen/attrdefs.td

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// RUN: mlir-tblgen -gen-attrdef-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL
22
// RUN: mlir-tblgen -gen-attrdef-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEF
3-
// RUN: mlir-tblgen -gen-attrdef-list -I %S/../../include %s | FileCheck %s --check-prefix=LIST
43

54
include "mlir/IR/AttrTypeBase.td"
65
include "mlir/IR/OpBase.td"
@@ -20,13 +19,6 @@ include "mlir/IR/OpBase.td"
2019
// DEF: ::test::CompoundAAttr,
2120
// DEF: ::test::SingleParameterAttr
2221

23-
// LIST: ATTRDEF(IndexAttr)
24-
// LIST: ATTRDEF(SimpleAAttr)
25-
// LIST: ATTRDEF(CompoundAAttr)
26-
// LIST: ATTRDEF(SingleParameterAttr)
27-
28-
// LIST: #undef ATTRDEF
29-
3022
// DEF-LABEL: ::mlir::OptionalParseResult generatedAttributeParser(
3123
// DEF-SAME: ::mlir::AsmParser &parser,
3224
// DEF-SAME: ::llvm::StringRef *mnemonic, ::mlir::Type type,

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,23 +1026,6 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
10261026
return false;
10271027
}
10281028

1029-
bool DefGenerator::emitList(StringRef selectedDialect) {
1030-
emitSourceFileHeader(("List of " + defType + "Def Definitions").str(), os);
1031-
1032-
SmallVector<AttrOrTypeDef, 16> defs;
1033-
collectAllDefs(selectedDialect, defRecords, defs);
1034-
if (defs.empty())
1035-
return false;
1036-
1037-
auto interleaveFn = [&](const AttrOrTypeDef &def) {
1038-
os << defType.upper() << "DEF(" << def.getCppClassName() << ")";
1039-
};
1040-
llvm::interleave(defs, os, interleaveFn, "\n");
1041-
os << "\n\n";
1042-
os << "#undef " << defType.upper() << "DEF" << "\n";
1043-
return false;
1044-
}
1045-
10461029
//===----------------------------------------------------------------------===//
10471030
// Type Constraints
10481031
//===----------------------------------------------------------------------===//
@@ -1117,12 +1100,6 @@ static mlir::GenRegistration
11171100
AttrDefGenerator generator(records, os);
11181101
return generator.emitDecls(attrDialect);
11191102
});
1120-
static mlir::GenRegistration
1121-
genAttrList("gen-attrdef-list", "Generate an AttrDef list",
1122-
[](const RecordKeeper &records, raw_ostream &os) {
1123-
AttrDefGenerator generator(records, os);
1124-
return generator.emitList(attrDialect);
1125-
});
11261103

11271104
//===----------------------------------------------------------------------===//
11281105
// TypeDef

0 commit comments

Comments
 (0)