Skip to content

Commit 684f140

Browse files
committed
merge main into amd-staging
2 parents a470909 + 1ff5c89 commit 684f140

File tree

138 files changed

+2559
-331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+2559
-331
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
330330
"zero expects struct, array, vector, or complex type");
331331
}
332332

333+
if (mlir::isa<cir::UndefAttr>(attrType)) {
334+
if (!mlir::isa<cir::VoidType>(opType))
335+
return success();
336+
return op->emitOpError("undef expects non-void type");
337+
}
338+
333339
if (mlir::isa<cir::BoolAttr>(attrType)) {
334340
if (!mlir::isa<cir::BoolType>(opType))
335341
return op->emitOpError("result type (")

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class CIRAttrToValue {
240240
.Case<cir::IntAttr, cir::FPAttr, cir::ConstComplexAttr,
241241
cir::ConstArrayAttr, cir::ConstRecordAttr, cir::ConstVectorAttr,
242242
cir::ConstPtrAttr, cir::GlobalViewAttr, cir::TypeInfoAttr,
243-
cir::VTableAttr, cir::ZeroAttr>(
243+
cir::UndefAttr, cir::VTableAttr, cir::ZeroAttr>(
244244
[&](auto attrT) { return visitCirAttr(attrT); })
245245
.Default([&](auto attrT) { return mlir::Value(); });
246246
}
@@ -254,6 +254,7 @@ class CIRAttrToValue {
254254
mlir::Value visitCirAttr(cir::ConstVectorAttr attr);
255255
mlir::Value visitCirAttr(cir::GlobalViewAttr attr);
256256
mlir::Value visitCirAttr(cir::TypeInfoAttr attr);
257+
mlir::Value visitCirAttr(cir::UndefAttr attr);
257258
mlir::Value visitCirAttr(cir::VTableAttr attr);
258259
mlir::Value visitCirAttr(cir::ZeroAttr attr);
259260

@@ -591,6 +592,13 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::TypeInfoAttr typeInfoAttr) {
591592
return result;
592593
}
593594

595+
/// UndefAttr visitor.
596+
mlir::Value CIRAttrToValue::visitCirAttr(cir::UndefAttr undefAttr) {
597+
mlir::Location loc = parentOp->getLoc();
598+
return mlir::LLVM::UndefOp::create(
599+
rewriter, loc, converter->convertType(undefAttr.getType()));
600+
}
601+
594602
// VTableAttr visitor.
595603
mlir::Value CIRAttrToValue::visitCirAttr(cir::VTableAttr vtableArr) {
596604
mlir::Type llvmTy = converter->convertType(vtableArr.getType());
@@ -2046,9 +2054,11 @@ CIRToLLVMGlobalOpLowering::matchAndRewriteRegionInitializedGlobal(
20462054
cir::GlobalOp op, mlir::Attribute init,
20472055
mlir::ConversionPatternRewriter &rewriter) const {
20482056
// TODO: Generalize this handling when more types are needed here.
2049-
assert((isa<cir::ConstArrayAttr, cir::ConstRecordAttr, cir::ConstVectorAttr,
2050-
cir::ConstPtrAttr, cir::ConstComplexAttr, cir::GlobalViewAttr,
2051-
cir::TypeInfoAttr, cir::VTableAttr, cir::ZeroAttr>(init)));
2057+
assert(
2058+
(isa<cir::ConstArrayAttr, cir::ConstRecordAttr, cir::ConstVectorAttr,
2059+
cir::ConstPtrAttr, cir::ConstComplexAttr, cir::GlobalViewAttr,
2060+
cir::TypeInfoAttr, cir::UndefAttr, cir::VTableAttr, cir::ZeroAttr>(
2061+
init)));
20522062

20532063
// TODO(cir): once LLVM's dialect has proper equivalent attributes this
20542064
// should be updated. For now, we use a custom op to initialize globals
@@ -2106,8 +2116,8 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
21062116
} else if (mlir::isa<cir::ConstArrayAttr, cir::ConstVectorAttr,
21072117
cir::ConstRecordAttr, cir::ConstPtrAttr,
21082118
cir::ConstComplexAttr, cir::GlobalViewAttr,
2109-
cir::TypeInfoAttr, cir::VTableAttr, cir::ZeroAttr>(
2110-
init.value())) {
2119+
cir::TypeInfoAttr, cir::UndefAttr, cir::VTableAttr,
2120+
cir::ZeroAttr>(init.value())) {
21112121
// TODO(cir): once LLVM's dialect has proper equivalent attributes this
21122122
// should be updated. For now, we use a custom op to initialize globals
21132123
// to the appropriate value.

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
11341134
CodeGenOpts.SanitizeMinimalRuntime),
11351135
/*MayReturn=*/
11361136
CodeGenOpts.SanitizeRecover.has(SanitizerKind::LocalBounds),
1137+
/*HandlerPreserveAllRegs=*/
1138+
static_cast<bool>(CodeGenOpts.SanitizeHandlerPreserveAllRegs),
11371139
};
11381140
}
11391141
FPM.addPass(BoundsCheckingPass(Options));

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3819,6 +3819,8 @@ static void emitCheckHandlerCall(CodeGenFunction &CGF,
38193819
bool NeedsAbortSuffix =
38203820
IsFatal && RecoverKind != CheckRecoverableKind::Unrecoverable;
38213821
bool MinimalRuntime = CGF.CGM.getCodeGenOpts().SanitizeMinimalRuntime;
3822+
bool HandlerPreserveAllRegs =
3823+
CGF.CGM.getCodeGenOpts().SanitizeHandlerPreserveAllRegs;
38223824
const SanitizerHandlerInfo &CheckInfo = SanitizerHandlers[CheckHandler];
38233825
const StringRef CheckName = CheckInfo.Name;
38243826
std::string FnName = "__ubsan_handle_" + CheckName.str();
@@ -3828,6 +3830,8 @@ static void emitCheckHandlerCall(CodeGenFunction &CGF,
38283830
FnName += "_minimal";
38293831
if (NeedsAbortSuffix)
38303832
FnName += "_abort";
3833+
if (HandlerPreserveAllRegs && !NeedsAbortSuffix)
3834+
FnName += "_preserve";
38313835
bool MayReturn =
38323836
!IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable;
38333837

@@ -3848,6 +3852,10 @@ static void emitCheckHandlerCall(CodeGenFunction &CGF,
38483852
(CGF.CurCodeDecl && CGF.CurCodeDecl->hasAttr<OptimizeNoneAttr>());
38493853
if (NoMerge)
38503854
HandlerCall->addFnAttr(llvm::Attribute::NoMerge);
3855+
if (HandlerPreserveAllRegs && !NeedsAbortSuffix) {
3856+
// N.B. there is also a clang::CallingConv which is not what we want here.
3857+
HandlerCall->setCallingConv(llvm::CallingConv::PreserveAll);
3858+
}
38513859
if (!MayReturn) {
38523860
HandlerCall->setDoesNotReturn();
38533861
CGF.Builder.CreateUnreachable();

clang/lib/CodeGen/CGPointerAuth.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,10 @@ CodeGenModule::getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
440440
IntegerDiscriminator = llvm::ConstantInt::get(Int64Ty, 0);
441441
}
442442

443-
return llvm::ConstantPtrAuth::get(Pointer,
444-
llvm::ConstantInt::get(Int32Ty, Key),
445-
IntegerDiscriminator, AddressDiscriminator);
443+
return llvm::ConstantPtrAuth::get(
444+
Pointer, llvm::ConstantInt::get(Int32Ty, Key), IntegerDiscriminator,
445+
AddressDiscriminator,
446+
/*DeactivationSymbol=*/llvm::Constant::getNullValue(DefaultPtrTy));
446447
}
447448

448449
/// Does a given PointerAuthScheme require us to sign a value

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,16 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
419419
const Driver &D = TC.getDriver();
420420
SanitizerMask TrappingKinds = parseSanitizeTrapArgs(D, Args, DiagnoseErrors);
421421
SanitizerMask InvalidTrappingKinds = TrappingKinds & NotAllowedWithTrap;
422+
const llvm::Triple &Triple = TC.getTriple();
422423

423424
MinimalRuntime =
424425
Args.hasFlag(options::OPT_fsanitize_minimal_runtime,
425426
options::OPT_fno_sanitize_minimal_runtime, MinimalRuntime);
426427
HandlerPreserveAllRegs =
427428
Args.hasFlag(options::OPT_fsanitize_handler_preserve_all_regs,
428429
options::OPT_fno_sanitize_handler_preserve_all_regs,
429-
HandlerPreserveAllRegs);
430+
HandlerPreserveAllRegs) &&
431+
MinimalRuntime && (Triple.isAArch64() || Triple.isX86_64());
430432

431433
// The object size sanitizer should not be enabled at -O0.
432434
Arg *OptLevel = Args.getLastArg(options::OPT_O_Group);
@@ -494,7 +496,6 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
494496
// -fsanitize=function and -fsanitize=kcfi instrument indirect function
495497
// calls to load a type hash before the function label. Therefore, an
496498
// execute-only target doesn't support the function and kcfi sanitizers.
497-
const llvm::Triple &Triple = TC.getTriple();
498499
if (isExecuteOnlyTarget(Triple, Args)) {
499500
if (SanitizerMask KindsToDiagnose =
500501
Add & NotAllowedWithExecuteOnly & ~DiagnosedKinds) {

clang/lib/Driver/ToolChains/Linux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ SanitizerMask Linux::getSupportedSanitizers() const {
11561156
if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ ||
11571157
IsLoongArch64 || IsRISCV64)
11581158
Res |= SanitizerKind::Thread;
1159-
if (IsX86_64 || IsAArch64 || IsSystemZ)
1159+
if (IsX86_64 || IsAArch64)
11601160
Res |= SanitizerKind::Type;
11611161
if (IsX86_64 || IsSystemZ || IsPowerPC64)
11621162
Res |= SanitizerKind::KernelMemory;

clang/test/CIR/CodeGen/lambda.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,39 @@
88
// We declare anonymous record types to represent lambdas. Rather than trying to
99
// to match the declarations, we establish variables for these when they are used.
1010

11+
auto global_lambda = [](){};
12+
void use_global_lambda() {
13+
global_lambda();
14+
}
15+
16+
// CIR: cir.global "private" internal dso_local @global_lambda = #cir.undef : ![[REC_LAM_GLOBAL_LAMBDA:.*]] {alignment = 1 : i64}
17+
// CIR: cir.func lambda internal private dso_local @_ZNK3$_0clEv(%[[THIS_ARG:.*]]: !cir.ptr<![[REC_LAM_GLOBAL_LAMBDA]]> {{.*}})
18+
// CIR: %[[THIS:.*]] = cir.alloca !cir.ptr<![[REC_LAM_GLOBAL_LAMBDA]]>, !cir.ptr<!cir.ptr<![[REC_LAM_GLOBAL_LAMBDA]]>>, ["this", init]
19+
// CIR: cir.store %[[THIS_ARG]], %[[THIS]]
20+
// CIR: cir.load %[[THIS]]
21+
//
22+
// CIR: cir.func {{.*}} @_Z17use_global_lambdav()
23+
// CIR: %[[LAMBDA:.*]] = cir.get_global @global_lambda : !cir.ptr<![[REC_LAM_GLOBAL_LAMBDA]]>
24+
// CIR: cir.call @_ZNK3$_0clEv(%[[LAMBDA]]) : (!cir.ptr<![[REC_LAM_GLOBAL_LAMBDA]]>) -> ()
25+
26+
// LLVM: @global_lambda = internal global %[[REC_LAM_GLOBAL_LAMBDA:.*]] undef, align 1
27+
// LLVM: define internal void @"_ZNK3$_0clEv"(ptr %[[THIS_ARG:.*]])
28+
// LLVM: %[[THIS_ADDR:.*]] = alloca ptr
29+
// LLVM: store ptr %[[THIS_ARG]], ptr %[[THIS_ADDR]]
30+
// LLVM: %[[THIS:.*]] = load ptr, ptr %[[THIS_ADDR]]
31+
//
32+
// LLVM: define dso_local void @_Z17use_global_lambdav()
33+
// LLVM: call void @"_ZNK3$_0clEv"(ptr @global_lambda)
34+
35+
// OGCG: @global_lambda = internal global %[[REC_LAM_GLOBAL_LAMBDA:.*]] undef, align 1
36+
// OGCG: define dso_local void @_Z17use_global_lambdav()
37+
// OGCG: call void @"_ZNK3$_0clEv"(ptr noundef nonnull align 1 dereferenceable(1) @global_lambda)
38+
//
39+
// OGCG: define internal void @"_ZNK3$_0clEv"(ptr {{.*}} %[[THIS_ARG:.*]])
40+
// OGCG: %[[THIS_ADDR:.*]] = alloca ptr
41+
// OGCG: store ptr %[[THIS_ARG]], ptr %[[THIS_ADDR]]
42+
// OGCG: %[[THIS:.*]] = load ptr, ptr %[[THIS_ADDR]]
43+
1144
void fn() {
1245
auto a = [](){};
1346
a();

0 commit comments

Comments
 (0)