clang 24.0.0git
RecordTypeConverter.h
Go to the documentation of this file.
1//===- RecordTypeConverter.h - Record-rebuilding type converter -*- 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// A type converter base for CIR-to-CIR passes that rewrite types nested in
10// records.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_LIB_CIR_DIALECT_TRANSFORMS_RECORDTYPECONVERTER_H
15#define CLANG_LIB_CIR_DIALECT_TRANSFORMS_RECORDTYPECONVERTER_H
16
17#include "mlir/Transforms/DialectConversion.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/Support/RWMutex.h"
22
23#include <memory>
24
25namespace cir {
26
27/// A type converter that converts the types nested in pointers, arrays,
28/// functions and records.
29///
30/// Identified records can't be modified in place, so each one is rebuilt under
31/// a temporary name (see RecordType::getABIConvertedName). The conversion keeps
32/// a stack of the records being converted, which resolves recursive records.
33/// Once the conversion is done, restoreRecordTypeNames() gives the rebuilt
34/// records their original names back.
35///
36/// Subclasses add conversions for the types they rewrite. The type converter
37/// tries the most recently added conversion first, so a subclass can also
38/// replace one of the conversions registered here.
39class RecordRewritingTypeConverter : public mlir::TypeConverter {
40public:
41 explicit RecordRewritingTypeConverter(mlir::MLIRContext &context);
42 virtual ~RecordRewritingTypeConverter() = default;
43
44 /// Remove the temporary name of every record rebuilt by this converter.
46
47private:
48 cir::RecordType convertRecordType(cir::RecordType type);
49 llvm::SmallVector<mlir::Type> convertRecordMemberTypes(cir::RecordType type);
50 llvm::SmallVector<cir::RecordType> &getCurrentThreadRecursiveStack();
51 void addConvertedRecordType(cir::RecordType rt);
52
53 mlir::MLIRContext &context;
54
55 // Recursive structure detection.
56 // We store one entry per thread here, and rely on locking. This works the
57 // same way as the LLVM-IR lowering does it, which has a similar problem.
58 llvm::DenseMap<uint64_t, std::unique_ptr<llvm::SmallVector<cir::RecordType>>>
59 conversionCallStack;
60 llvm::sys::SmartRWMutex<true> callStackMutex;
61
62 // In order to let us 'change the names' back after the fact, we collect them
63 // along the way. They should only be added/accessed via the thread-safe
64 // functions.
65 llvm::SmallVector<cir::RecordType> convertedRecordTypes;
66 llvm::sys::SmartRWMutex<true> recordTypeMutex;
67};
68
69} // namespace cir
70
71#endif // CLANG_LIB_CIR_DIALECT_TRANSFORMS_RECORDTYPECONVERTER_H
void restoreRecordTypeNames()
Remove the temporary name of every record rebuilt by this converter.
virtual ~RecordRewritingTypeConverter()=default
RecordRewritingTypeConverter(mlir::MLIRContext &context)
C++ view class that accepts both !cir.struct and !cir.union types.
Definition CIRTypes.h:149