Skip to content

Commit b71210f

Browse files
committed
[LLVMABI] Added mappings for record types
1 parent 3a58271 commit b71210f

File tree

6 files changed

+116
-105
lines changed

6 files changed

+116
-105
lines changed

clang/include/clang/ABI/QualTypeMapper.h renamed to clang/include/clang/CodeGen/QualtypeMapper.h

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
1-
#ifndef LLVM_ABI_QUALTYPE_MAPPER_H
2-
#define LLVM_ABI_QUALTYPE_MAPPER_H
3-
4-
#include "llvm/IR/DerivedTypes.h"
1+
//==---- QualtypeMapper.h - Maps Clang Qualtype to LLVMABI Types -----------==//
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+
/// \file
10+
/// Maps Clang QualType instances to corresponding LLVM ABI type
11+
/// representations. This mapper translates high-level type information from the
12+
/// AST into low-level ABI-specific types that encode size, alignment, and
13+
/// layout details required for code generation and cross-language
14+
/// interoperability.
15+
///
16+
//===----------------------------------------------------------------------===//
17+
#ifndef CLANG_CODEGEN_QUALTYPE_MAPPER_H
18+
#define CLANG_CODEGEN_QUALTYPE_MAPPER_H
19+
20+
#include "clang/AST/ASTContext.h"
21+
#include "clang/AST/Decl.h"
22+
#include "clang/AST/Type.h"
23+
#include "clang/AST/TypeOrdering.h"
24+
#include "llvm/ABI/Types.h"
25+
#include "llvm/ADT/DenseMap.h"
526
#include "llvm/Support/Allocator.h"
6-
#include <clang/AST/ASTContext.h>
7-
#include <clang/AST/Decl.h>
8-
#include <clang/AST/Type.h>
9-
#include <llvm/ABI/Types.h>
10-
#include <llvm/ADT/DenseMap.h>
11-
12-
// Specialization for QualType
13-
template <> struct llvm::DenseMapInfo<clang::QualType> {
14-
static inline clang::QualType getEmptyKey() {
15-
return clang::QualType::getFromOpaquePtr(
16-
reinterpret_cast<clang::Type *>(-1));
17-
}
18-
19-
static inline clang::QualType getTombstoneKey() {
20-
return clang::QualType::getFromOpaquePtr(
21-
reinterpret_cast<clang::Type *>(-2));
22-
}
23-
24-
static unsigned getHashValue(const clang::QualType &Val) {
25-
return (unsigned)((uintptr_t)Val.getAsOpaquePtr()) ^
26-
((unsigned)((uintptr_t)Val.getAsOpaquePtr() >> 9));
27-
}
28-
29-
static bool isEqual(const clang::QualType &LHS, const clang::QualType &RHS) {
30-
return LHS == RHS;
31-
}
32-
};
3327

3428
namespace clang {
3529
namespace mapper {
@@ -75,4 +69,4 @@ class QualTypeMapper {
7569
} // namespace mapper
7670
} // namespace clang
7771

78-
#endif // !LLVM_ABI_QUALTYPE_MAPPER_H
72+
#endif // !CLANG_CODEGEN_QUALTYPE_MAPPER_H

clang/lib/ABI/CMakeLists.txt

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

clang/lib/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
add_subdirectory(ABI)
21
add_subdirectory(Headers)
32
add_subdirectory(Basic)
43
add_subdirectory(APINotes)

clang/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ add_clang_library(clangCodeGen
115115
ModuleBuilder.cpp
116116
ObjectFilePCHContainerWriter.cpp
117117
PatternInit.cpp
118+
QualtypeMapper.cpp
118119
SanitizerMetadata.cpp
119120
SwiftCallingConv.cpp
120121
TargetBuiltins/ARM.cpp

clang/lib/ABI/QualTypeMapper.cpp renamed to clang/lib/CodeGen/QualtypeMapper.cpp

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1+
//==---- QualtypeMapper.cpp - Maps Clang Qualtype to LLVMABI Types ---------==//
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+
/// \file
10+
/// Maps Clang QualType instances to corresponding LLVM ABI type
11+
/// representations. This mapper translates high-level type information from the
12+
/// AST into low-level ABI-specific types that encode size, alignment, and
13+
/// layout details required for code generation and cross-language
14+
/// interoperability.
15+
///
16+
//===----------------------------------------------------------------------===//
17+
#include "clang/CodeGen/QualtypeMapper.h"
18+
#include "clang/AST/Decl.h"
119
#include "clang/AST/RecordLayout.h"
220
#include "clang/AST/Type.h"
3-
#include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
421
#include "clang/Basic/LLVM.h"
522
#include "clang/Basic/TargetInfo.h"
623
#include "llvm/ABI/Types.h"
724
#include "llvm/Support/Alignment.h"
8-
#include "llvm/Support/Casting.h"
9-
#include <clang/ABI/QualTypeMapper.h>
25+
#include "llvm/Support/TypeSize.h"
1026

1127
namespace clang {
1228
namespace mapper {
@@ -40,87 +56,44 @@ const llvm::abi::Type *QualTypeMapper::convertType(QualType QT) {
4056

4157
const llvm::abi::Type *
4258
QualTypeMapper::convertBuiltinType(const BuiltinType *BT) {
59+
QualType QT(BT, 0);
60+
4361
switch (BT->getKind()) {
4462
case BuiltinType::Void:
4563
return Builder.getVoidType();
4664

4765
case BuiltinType::Bool:
48-
case BuiltinType::UChar:
49-
case BuiltinType::Char_U:
50-
case BuiltinType::UShort:
51-
return Builder.getIntegerType(ASTCtx.getTypeSize(QualType(BT, 0)),
52-
getTypeAlign(QualType(BT, 0)), false);
53-
5466
case BuiltinType::Char_S:
67+
case BuiltinType::Char_U:
5568
case BuiltinType::SChar:
56-
case BuiltinType::Short:
57-
return Builder.getIntegerType(ASTCtx.getCharWidth(),
58-
getTypeAlign(QualType(BT, 0)), true);
59-
60-
case BuiltinType::WChar_U:
61-
return Builder.getIntegerType(ASTCtx.getCharWidth(),
62-
getTypeAlign(QualType(BT, 0)), false);
63-
69+
case BuiltinType::UChar:
6470
case BuiltinType::WChar_S:
65-
return Builder.getIntegerType(ASTCtx.getCharWidth(),
66-
getTypeAlign(QualType(BT, 0)), true);
67-
71+
case BuiltinType::WChar_U:
6872
case BuiltinType::Char8:
69-
return Builder.getIntegerType(8, getTypeAlign(QualType(BT, 0)), false);
70-
7173
case BuiltinType::Char16:
72-
return Builder.getIntegerType(16, getTypeAlign(QualType(BT, 0)), false);
73-
7474
case BuiltinType::Char32:
75-
return Builder.getIntegerType(32, getTypeAlign(QualType(BT, 0)), false);
76-
75+
case BuiltinType::Short:
76+
case BuiltinType::UShort:
7777
case BuiltinType::Int:
7878
case BuiltinType::UInt:
79-
return Builder.getIntegerType(ASTCtx.getIntWidth(QualType(BT, 0)),
80-
getTypeAlign(QualType(BT, 0)),
81-
BT->getKind() == BuiltinType::Int);
82-
8379
case BuiltinType::Long:
8480
case BuiltinType::ULong:
85-
return Builder.getIntegerType(ASTCtx.getTypeSize(QualType(BT, 0)),
86-
getTypeAlign(QualType(BT, 0)),
87-
BT->getKind() == BuiltinType::Long);
88-
8981
case BuiltinType::LongLong:
9082
case BuiltinType::ULongLong:
91-
return Builder.getIntegerType(ASTCtx.getTypeSize(QualType(BT, 0)),
92-
getTypeAlign(QualType(BT, 0)),
93-
BT->getKind() == BuiltinType::LongLong);
94-
9583
case BuiltinType::Int128:
9684
case BuiltinType::UInt128:
97-
return Builder.getIntegerType(128, getTypeAlign(QualType(BT, 0)),
98-
BT->getKind() == BuiltinType::Int128);
85+
return Builder.getIntegerType(ASTCtx.getTypeSize(QT), getTypeAlign(QT),
86+
BT->isSignedInteger());
9987

10088
case BuiltinType::Half:
10189
case BuiltinType::Float16:
102-
return Builder.getFloatType(llvm::APFloat::IEEEhalf(),
103-
getTypeAlign(QualType(BT, 0)));
104-
90+
case BuiltinType::BFloat16:
10591
case BuiltinType::Float:
106-
return Builder.getFloatType(llvm::APFloat::IEEEsingle(),
107-
getTypeAlign(QualType(BT, 0)));
108-
10992
case BuiltinType::Double:
110-
return Builder.getFloatType(llvm::APFloat::IEEEdouble(),
111-
getTypeAlign(QualType(BT, 0)));
112-
11393
case BuiltinType::LongDouble:
114-
return Builder.getFloatType(ASTCtx.getFloatTypeSemantics(QualType(BT, 0)),
115-
getTypeAlign(QualType(BT, 0)));
116-
117-
case BuiltinType::BFloat16:
118-
return Builder.getFloatType(llvm::APFloat::BFloat(),
119-
getTypeAlign(QualType(BT, 0)));
120-
12194
case BuiltinType::Float128:
122-
return Builder.getFloatType(llvm::APFloat::IEEEquad(),
123-
getTypeAlign(QualType(BT, 0)));
95+
return Builder.getFloatType(ASTCtx.getFloatTypeSemantics(QT),
96+
getTypeAlign(QT));
12497

12598
default:
12699
return Builder.getIntegerType(ASTCtx.getTypeSize(QualType(BT, 0)),
@@ -171,6 +144,45 @@ QualTypeMapper::convertPointerType(const clang::PointerType *PT) {
171144
return createPointerTypeForPointee(PT->getPointeeType());
172145
}
173146

147+
const llvm::abi::Type *
148+
QualTypeMapper::convertEnumType(const clang::EnumType *ET) {
149+
const EnumDecl *ED = ET->getDecl();
150+
QualType UnderlyingType = ED->getIntegerType();
151+
152+
if (UnderlyingType.isNull())
153+
UnderlyingType = ASTCtx.IntTy;
154+
155+
return convertType(UnderlyingType);
156+
}
157+
158+
const llvm::abi::StructType *
159+
QualTypeMapper::convertStructType(const clang::RecordDecl *RD) {
160+
const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(RD);
161+
162+
SmallVector<llvm::abi::FieldInfo, 16> Fields;
163+
computeFieldInfo(RD, Fields, Layout);
164+
165+
llvm::TypeSize Size =
166+
llvm::TypeSize::getFixed(Layout.getSize().getQuantity() * 8);
167+
llvm::Align Alignment = llvm::Align(Layout.getAlignment().getQuantity());
168+
169+
return Builder.getStructType(Fields, Size, Alignment);
170+
}
171+
172+
const llvm::abi::UnionType *
173+
QualTypeMapper::convertUnionType(const clang::RecordDecl *RD) {
174+
const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(RD);
175+
176+
SmallVector<llvm::abi::FieldInfo, 16> Fields;
177+
computeFieldInfo(RD, Fields, Layout);
178+
179+
llvm::TypeSize Size =
180+
llvm::TypeSize::getFixed(Layout.getSize().getQuantity() * 8);
181+
llvm::Align Alignment = llvm::Align(Layout.getAlignment().getQuantity());
182+
183+
return Builder.getUnionType(Fields, Size, Alignment);
184+
}
185+
174186
llvm::Align QualTypeMapper::getTypeAlign(QualType QT) const {
175187
return llvm::Align(ASTCtx.getTypeAlign(QT));
176188
}

llvm/include/llvm/ABI/Types.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
//===- ABI/Types.h ----------------------------------------------*- 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+
/// \file
10+
/// This file defines the Types and related helper methods concerned to the
11+
/// LLVMABI library which mirrors ABI related type information from
12+
/// the LLVM frontend.
13+
///
14+
//===----------------------------------------------------------------------===//
115
#ifndef LLVM_ABI_TYPES_H
216
#define LLVM_ABI_TYPES_H
317

@@ -27,21 +41,21 @@ class Type {
2741
protected:
2842
TypeKind Kind;
2943
TypeSize SizeInBits;
30-
Align AlignInBits;
44+
Align Alignment;
3145
bool IsExplicitlyAligned;
3246

3347
Type(TypeKind K, TypeSize Size, Align Align, bool ExplicitAlign = false)
34-
: Kind(K), SizeInBits(Size), AlignInBits(Align),
48+
: Kind(K), SizeInBits(Size), Alignment(Align),
3549
IsExplicitlyAligned(ExplicitAlign) {}
3650

3751
public:
3852
TypeKind getKind() const { return Kind; }
3953
TypeSize getSizeInBits() const { return SizeInBits; }
40-
Align getAlignInBits() const { return AlignInBits; }
54+
Align getAlignment() const { return Alignment; }
4155
bool hasExplicitAlignment() const { return IsExplicitlyAligned; }
4256

4357
void setExplicitAlignment(Align Align) {
44-
AlignInBits = Align;
58+
Alignment = Align;
4559
IsExplicitlyAligned = true;
4660
}
4761

@@ -109,7 +123,7 @@ class ArrayType : public Type {
109123
public:
110124
ArrayType(const Type *ElemType, uint64_t NumElems)
111125
: Type(TypeKind::Array, ElemType->getSizeInBits() * NumElems,
112-
ElemType->getAlignInBits()),
126+
ElemType->getAlignment()),
113127
ElementType(ElemType), NumElements(NumElems) {}
114128

115129
const Type *getElementType() const { return ElementType; }

0 commit comments

Comments
 (0)