Skip to content

Commit cd7b12e

Browse files
sitio-coutolanza
authored andcommitted
[CIR][ABI] Replay TargetLowering library reverted commits (#697)
Essentially re-applies #668 and #678, but also includes #687 which patched build introduced by the other two PRs. Closes #691
1 parent 4923fdf commit cd7b12e

33 files changed

+1305
-60
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mlir/Dialect/DLTI/DLTI.h"
1616
#include "mlir/IR/BuiltinOps.h"
1717
#include "clang/CIR/Dialect/IR/CIRTypes.h"
18+
#include "llvm/ADT/StringRef.h"
1819

1920
namespace cir {
2021

@@ -24,7 +25,20 @@ class CIRDataLayout {
2425
public:
2526
mlir::DataLayout layout;
2627

28+
/// Constructs a DataLayout from a specification string. See reset().
29+
explicit CIRDataLayout(llvm::StringRef dataLayout, mlir::ModuleOp module)
30+
: layout(module) {
31+
reset(dataLayout);
32+
}
33+
34+
/// Parse a data layout string (with fallback to default values).
35+
void reset(llvm::StringRef dataLayout);
36+
37+
// Free all internal data structures.
38+
void clear();
39+
2740
CIRDataLayout(mlir::ModuleOp modOp);
41+
2842
bool isBigEndian() const { return bigEndian; }
2943

3044
// `useABI` is `true` if not using prefered alignment.

clang/include/clang/CIR/FnInfoOpts.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef CIR_FNINFOOPTS_H
2+
#define CIR_FNINFOOPTS_H
3+
4+
#include "llvm/ADT/STLForwardCompat.h"
5+
6+
namespace cir {
7+
8+
enum class FnInfoOpts {
9+
None = 0,
10+
IsInstanceMethod = 1 << 0,
11+
IsChainCall = 1 << 1,
12+
IsDelegateCall = 1 << 2,
13+
};
14+
15+
inline FnInfoOpts operator|(FnInfoOpts A, FnInfoOpts B) {
16+
return static_cast<FnInfoOpts>(llvm::to_underlying(A) |
17+
llvm::to_underlying(B));
18+
}
19+
20+
inline FnInfoOpts operator&(FnInfoOpts A, FnInfoOpts B) {
21+
return static_cast<FnInfoOpts>(llvm::to_underlying(A) &
22+
llvm::to_underlying(B));
23+
}
24+
25+
inline FnInfoOpts operator|=(FnInfoOpts A, FnInfoOpts B) {
26+
A = A | B;
27+
return A;
28+
}
29+
30+
inline FnInfoOpts operator&=(FnInfoOpts A, FnInfoOpts B) {
31+
A = A & B;
32+
return A;
33+
}
34+
35+
} // namespace cir
36+
37+
#endif // CIR_FNINFOOPTS_H

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ struct MissingFeatures {
156156
static bool zeroInitializer() { return false; }
157157
static bool targetCodeGenInfoIsProtoCallVariadic() { return false; }
158158
static bool targetCodeGenInfoGetNullPointer() { return false; }
159-
static bool chainCalls() { return false; }
160159
static bool operandBundles() { return false; }
161160
static bool exceptions() { return false; }
162161
static bool metaDataNode() { return false; }
@@ -190,24 +189,61 @@ struct MissingFeatures {
190189

191190
//===--- ABI lowering --===//
192191

192+
//-- Missing AST queries
193+
194+
static bool recordDeclCanPassInRegisters() { return false; }
195+
static bool funcDeclIsCXXConstructorDecl() { return false; }
196+
static bool funcDeclIsCXXDestructorDecl() { return false; }
197+
static bool funcDeclIsCXXMethodDecl() { return false; }
198+
static bool funcDeclIsInlineBuiltinDeclaration() { return false; }
199+
static bool funcDeclIsReplaceableGlobalAllocationFunction() { return false; }
200+
static bool qualTypeIsReferenceType() { return false; }
201+
202+
//-- Missing types
203+
204+
static bool vectorType() { return false; }
205+
206+
//-- Missing LLVM attributes
207+
208+
static bool noReturn() { return false; }
209+
static bool csmeCall() { return false; }
210+
211+
//-- Other missing features
212+
213+
// Calls with a static chain pointer argument may be optimized (p.e. freeing
214+
// up argument registers), but we do not yet track such cases.
215+
static bool chainCall() { return false; }
216+
217+
// ABI-lowering has special handling for regcall calling convention (tries to
218+
// pass every argument in regs). We don't support it just yet.
219+
static bool regCall() { return false; }
220+
221+
// Some ABIs (e.g. x86) require special handling for returning large structs
222+
// by value. The sret argument parameter aids in this, but it is current NYI.
223+
static bool sretArgs() { return false; }
224+
225+
// Inalloca parameter attributes are mostly used for Windows x86_32 ABI. We
226+
// do not yet support this yet.
227+
static bool inallocaArgs() { return false; }
228+
193229
// Parameters may have additional attributes (e.g. [[noescape]]) that affect
194230
// the compiler. This is not yet supported in CIR.
195-
static bool extParamInfo() { return true; }
231+
static bool extParamInfo() { return false; }
196232

197233
// LangOpts may affect lowering, but we do not carry this information into CIR
198234
// just yet. Right now, it only instantiates the default lang options.
199-
static bool langOpts() { return true; }
235+
static bool langOpts() { return false; }
200236

201237
// Several type qualifiers are not yet supported in CIR, but important when
202238
// evaluating ABI-specific lowering.
203-
static bool qualifiedTypes() { return true; }
239+
static bool qualifiedTypes() { return false; }
204240

205241
// We're ignoring several details regarding ABI-halding for Swift.
206-
static bool swift() { return true; }
242+
static bool swift() { return false; }
207243

208244
// Despite carrying some information about variadics, we are currently
209245
// ignoring this to focus only on the code necessary to lower non-variadics.
210-
static bool variadicFunctions() { return true; }
246+
static bool variadicFunctions() { return false; }
211247
};
212248

213249
} // namespace cir

clang/include/clang/CIR/Target/x86.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//==-- x86.h - Definitions common to all x86 ABI variants ------------------==//
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+
// Definitions common to any X86 ABI implementation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef CIR_X86_H
14+
#define CIR_X86_H
15+
16+
namespace cir {
17+
18+
// Possible argument classifications according to the x86 ABI documentation.
19+
enum X86ArgClass {
20+
Integer = 0,
21+
SSE,
22+
SSEUp,
23+
X87,
24+
X87Up,
25+
ComplexX87,
26+
NoClass,
27+
Memory
28+
};
29+
30+
} // namespace cir
31+
32+
#endif // CIR_X86_H

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "clang/AST/GlobalDecl.h"
2424
#include "clang/CIR/Dialect/IR/CIRDialect.h"
2525
#include "clang/CIR/Dialect/IR/CIRTypes.h"
26+
#include "clang/CIR/FnInfoOpts.h"
2627
#include "llvm/Support/ErrorHandling.h"
2728
#include <cassert>
2829

clang/lib/CIR/CodeGen/CIRGenCall.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,6 @@ class ReturnValueSlot {
290290
Address getAddress() const { return Addr; }
291291
};
292292

293-
enum class FnInfoOpts {
294-
None = 0,
295-
IsInstanceMethod = 1 << 0,
296-
IsChainCall = 1 << 1,
297-
IsDelegateCall = 1 << 2,
298-
};
299-
300293
} // namespace cir
301294

302295
#endif

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ RValue CIRGenFunction::buildCall(clang::QualType CalleeType,
14391439
// Chain calls use the same code path to add the inviisble chain parameter to
14401440
// the function type.
14411441
if (isa<FunctionNoProtoType>(FnType) || Chain) {
1442-
assert(!MissingFeatures::chainCalls());
1442+
assert(!MissingFeatures::chainCall());
14431443
assert(!MissingFeatures::addressSpace());
14441444
auto CalleeTy = getTypes().GetFunctionType(FnInfo);
14451445
// get non-variadic function type

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "clang/AST/GlobalDecl.h"
1717
#include "clang/AST/RecordLayout.h"
1818
#include "clang/CIR/Dialect/IR/CIRTypes.h"
19+
#include "clang/CIR/FnInfoOpts.h"
1920
#include "llvm/ADT/STLExtras.h"
2021
#include "llvm/Support/Casting.h"
2122
#include "llvm/Support/ErrorHandling.h"

clang/lib/CIR/CodeGen/CIRGenTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "clang/AST/Type.h"
2323
#include "clang/Basic/ABI.h"
2424
#include "clang/CIR/Dialect/IR/CIRTypes.h"
25+
#include "clang/CIR/FnInfoOpts.h"
2526

2627
#include "llvm/ADT/SmallPtrSet.h"
2728

clang/lib/CIR/CodeGen/TargetInfo.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "CallingConv.h"
77

88
#include "clang/Basic/TargetInfo.h"
9+
#include "clang/CIR/Target/x86.h"
910

1011
using namespace cir;
1112
using namespace clang;
@@ -79,16 +80,7 @@ namespace {
7980
enum class X86AVXABILevel { None, AVX, AVX512 };
8081

8182
class X86_64ABIInfo : public ABIInfo {
82-
enum Class {
83-
Integer = 0,
84-
SSE,
85-
SSEUp,
86-
X87,
87-
X87Up,
88-
ComplexX87,
89-
NoClass,
90-
Memory
91-
};
83+
using Class = X86ArgClass;
9284

9385
// X86AVXABILevel AVXLevel;
9486
// Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on 64-bit

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
//===- CIRDialect.cpp - MLIR CIR ops implementation -----------------------===//
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-
// This file defines the CIR DataLayout class and its functions.
10-
//
11-
//===----------------------------------------------------------------------===//a
12-
//
13-
141
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
2+
#include "llvm/ADT/StringRef.h"
153

164
namespace cir {
5+
176
CIRDataLayout::CIRDataLayout(mlir::ModuleOp modOp) : layout{modOp} {
187
auto dlSpec = modOp->getAttr(mlir::DLTIDialect::kDataLayoutAttrName)
198
.dyn_cast<mlir::DataLayoutSpecAttr>();
@@ -40,4 +29,8 @@ CIRDataLayout::CIRDataLayout(mlir::ModuleOp modOp) : layout{modOp} {
4029
}
4130
}
4231

32+
void CIRDataLayout::reset(llvm::StringRef Desc) { clear(); }
33+
34+
void CIRDataLayout::clear() {}
35+
4336
} // namespace cir

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_clang_library(MLIRCIR
1717
LINK_LIBS PUBLIC
1818
MLIRIR
1919
MLIRCIRInterfaces
20+
MLIRDLTIDialect
2021
MLIRDataLayoutInterfaces
2122
MLIRFuncDialect
2223
MLIRLoopLikeInterface

clang/lib/CIR/Dialect/Transforms/CallConvLowering.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ LowerModule createLowerModule(FuncOp op, PatternRewriter &rewriter) {
4545
// FIXME(cir): This just uses the default language options. We need to account
4646
// for custom options.
4747
// Create context.
48-
assert(::cir::MissingFeatures::langOpts());
48+
assert(!::cir::MissingFeatures::langOpts());
4949
clang::LangOptions langOpts;
5050
auto context = CIRLowerContext(module.getContext(), langOpts);
5151
context.initBuiltinTypes(*targetInfo);
@@ -64,11 +64,29 @@ struct CallConvLoweringPattern : public OpRewritePattern<FuncOp> {
6464

6565
LogicalResult matchAndRewrite(FuncOp op,
6666
PatternRewriter &rewriter) const final {
67+
const auto module = op->getParentOfType<mlir::ModuleOp>();
68+
6769
if (!op.getAst())
6870
return op.emitError("function has no AST information");
6971

7072
LowerModule lowerModule = createLowerModule(op, rewriter);
7173

74+
// Rewrite function calls before definitions. This should be done before
75+
// lowering the definition.
76+
auto calls = op.getSymbolUses(module);
77+
if (calls.has_value()) {
78+
for (auto call : calls.value()) {
79+
auto callOp = cast<CallOp>(call.getUser());
80+
if (lowerModule.rewriteFunctionCall(callOp, op).failed())
81+
return failure();
82+
}
83+
}
84+
85+
// TODO(cir): Instead of re-emmiting every load and store, bitcast arguments
86+
// and return values to their ABI-specific counterparts when possible.
87+
if (lowerModule.rewriteFunctionDefinition(op).failed())
88+
return failure();
89+
7290
return success();
7391
}
7492
};

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "ABIInfo.h"
15+
#include "CIRCXXABI.h"
16+
#include "LowerTypes.h"
1517

1618
namespace mlir {
1719
namespace cir {
1820

1921
// Pin the vtable to this file.
2022
ABIInfo::~ABIInfo() = default;
2123

24+
CIRCXXABI &ABIInfo::getCXXABI() const { return LT.getCXXABI(); }
25+
2226
} // namespace cir
2327
} // namespace mlir

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#ifndef LLVM_CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_ABIINFO_H
1515
#define LLVM_CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_ABIINFO_H
1616

17+
#include "CIRCXXABI.h"
18+
#include "LowerFunctionInfo.h"
1719
#include "llvm/IR/CallingConv.h"
1820

1921
namespace mlir {
@@ -32,6 +34,10 @@ class ABIInfo {
3234
public:
3335
ABIInfo(LowerTypes &LT) : LT(LT), RuntimeCC(llvm::CallingConv::C) {}
3436
virtual ~ABIInfo();
37+
38+
CIRCXXABI &getCXXABI() const;
39+
40+
virtual void computeInfo(LowerFunctionInfo &FI) const = 0;
3541
};
3642

3743
} // namespace cir

clang/lib/CIR/Dialect/Transforms/TargetLowering/ABIInfoImpl.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,25 @@
1010
// adapted to operate on the CIR dialect, however.
1111
//
1212
//===----------------------------------------------------------------------===//
13+
14+
#include "ABIInfo.h"
15+
#include "CIRCXXABI.h"
16+
#include "LowerFunctionInfo.h"
17+
#include "llvm/Support/ErrorHandling.h"
18+
19+
namespace mlir {
20+
namespace cir {
21+
22+
bool classifyReturnType(const CIRCXXABI &CXXABI, LowerFunctionInfo &FI,
23+
const ABIInfo &Info) {
24+
Type Ty = FI.getReturnType();
25+
26+
if (const auto RT = Ty.dyn_cast<StructType>()) {
27+
llvm_unreachable("NYI");
28+
}
29+
30+
return CXXABI.classifyReturnType(FI);
31+
}
32+
33+
} // namespace cir
34+
} // namespace mlir

0 commit comments

Comments
 (0)