Skip to content

Commit 6073386

Browse files
authored
Merge branch 'main' into hlsl_dst_function
2 parents 93f8f19 + bd197ca commit 6073386

File tree

75 files changed

+1947
-325
lines changed

Some content is hidden

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

75 files changed

+1947
-325
lines changed

clang/include/clang/AST/Type.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,10 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
27602760
/// of some sort, e.g., it is a floating-point type or a vector thereof.
27612761
bool hasFloatingRepresentation() const;
27622762

2763+
/// Determine whether this type has a boolean representation
2764+
/// of some sort.
2765+
bool hasBooleanRepresentation() const;
2766+
27632767
// Type Checking Functions: Check to see if this type is structurally the
27642768
// specified type, ignoring typedefs and qualifiers, and return a pointer to
27652769
// the best type we can.

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4891,6 +4891,12 @@ def HLSLDotProduct : LangBuiltin<"HLSL_LANG"> {
48914891
let Prototype = "void(...)";
48924892
}
48934893

4894+
def HLSLDot2Add : LangBuiltin<"HLSL_LANG"> {
4895+
let Spellings = ["__builtin_hlsl_dot2add"];
4896+
let Attributes = [NoThrow, Const];
4897+
let Prototype = "float(_ExtVector<2, _Float16>, _ExtVector<2, _Float16>, float)";
4898+
}
4899+
48944900
def HLSLDot4AddI8Packed : LangBuiltin<"HLSL_LANG"> {
48954901
let Spellings = ["__builtin_hlsl_dot4add_i8packed"];
48964902
let Attributes = [NoThrow, Const];

clang/include/clang/Basic/DarwinSDKInfo.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,19 @@ class DarwinSDKInfo {
143143

144144
DarwinSDKInfo(
145145
VersionTuple Version, VersionTuple MaximumDeploymentTarget,
146+
llvm::Triple::OSType OS,
146147
llvm::DenseMap<OSEnvPair::StorageType,
147148
std::optional<RelatedTargetVersionMapping>>
148149
VersionMappings =
149150
llvm::DenseMap<OSEnvPair::StorageType,
150151
std::optional<RelatedTargetVersionMapping>>())
151152
: Version(Version), MaximumDeploymentTarget(MaximumDeploymentTarget),
152-
VersionMappings(std::move(VersionMappings)) {}
153+
OS(OS), VersionMappings(std::move(VersionMappings)) {}
153154

154155
const llvm::VersionTuple &getVersion() const { return Version; }
155156

157+
const llvm::Triple::OSType &getOS() const { return OS; }
158+
156159
// Returns the optional, target-specific version mapping that maps from one
157160
// target to another target.
158161
//
@@ -177,6 +180,7 @@ class DarwinSDKInfo {
177180
private:
178181
VersionTuple Version;
179182
VersionTuple MaximumDeploymentTarget;
183+
llvm::Triple::OSType OS;
180184
// Need to wrap the value in an optional here as the value has to be default
181185
// constructible, and std::unique_ptr doesn't like DarwinSDKInfo being
182186
// Optional as Optional is trying to copy it in emplace.

clang/lib/AST/Type.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,19 @@ bool Type::isArithmeticType() const {
23342334
return isa<ComplexType>(CanonicalType) || isBitIntType();
23352335
}
23362336

2337+
bool Type::hasBooleanRepresentation() const {
2338+
if (isBooleanType())
2339+
return true;
2340+
2341+
if (const EnumType *ET = getAs<EnumType>())
2342+
return ET->getDecl()->getIntegerType()->isBooleanType();
2343+
2344+
if (const AtomicType *AT = getAs<AtomicType>())
2345+
return AT->getValueType()->hasBooleanRepresentation();
2346+
2347+
return false;
2348+
}
2349+
23372350
Type::ScalarTypeKind Type::getScalarTypeKind() const {
23382351
assert(isScalarType());
23392352

clang/lib/Basic/DarwinSDKInfo.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Basic/DarwinSDKInfo.h"
10+
#include "llvm/ADT/StringSwitch.h"
1011
#include "llvm/Support/ErrorOr.h"
1112
#include "llvm/Support/JSON.h"
1213
#include "llvm/Support/MemoryBuffer.h"
@@ -62,6 +63,28 @@ DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(
6263
Min, Max, MinValue, MaximumDeploymentTarget, std::move(Mapping));
6364
}
6465

66+
static llvm::Triple::OSType parseOS(const llvm::json::Object &Obj) {
67+
// The CanonicalName is the Xcode platform followed by a version, e.g.
68+
// macosx16.0.
69+
auto CanonicalName = Obj.getString("CanonicalName");
70+
if (!CanonicalName)
71+
return llvm::Triple::UnknownOS;
72+
size_t VersionStart = CanonicalName->find_first_of("0123456789");
73+
StringRef XcodePlatform = CanonicalName->slice(0, VersionStart);
74+
return llvm::StringSwitch<llvm::Triple::OSType>(XcodePlatform)
75+
.Case("macosx", llvm::Triple::MacOSX)
76+
.Case("iphoneos", llvm::Triple::IOS)
77+
.Case("iphonesimulator", llvm::Triple::IOS)
78+
.Case("appletvos", llvm::Triple::TvOS)
79+
.Case("appletvsimulator", llvm::Triple::TvOS)
80+
.Case("watchos", llvm::Triple::WatchOS)
81+
.Case("watchsimulator", llvm::Triple::WatchOS)
82+
.Case("xros", llvm::Triple::XROS)
83+
.Case("xrsimulator", llvm::Triple::XROS)
84+
.Case("driverkit", llvm::Triple::DriverKit)
85+
.Default(llvm::Triple::UnknownOS);
86+
}
87+
6588
static std::optional<VersionTuple> getVersionKey(const llvm::json::Object &Obj,
6689
StringRef Key) {
6790
auto Value = Obj.getString(Key);
@@ -82,6 +105,7 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj) {
82105
getVersionKey(*Obj, "MaximumDeploymentTarget");
83106
if (!MaximumDeploymentVersion)
84107
return std::nullopt;
108+
llvm::Triple::OSType OS = parseOS(*Obj);
85109
llvm::DenseMap<OSEnvPair::StorageType,
86110
std::optional<RelatedTargetVersionMapping>>
87111
VersionMappings;
@@ -124,7 +148,7 @@ DarwinSDKInfo::parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj) {
124148
}
125149

126150
return DarwinSDKInfo(std::move(*Version),
127-
std::move(*MaximumDeploymentVersion),
151+
std::move(*MaximumDeploymentVersion), OS,
128152
std::move(VersionMappings));
129153
}
130154

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
160160
llvm_unreachable("negation for the given type is NYI");
161161
}
162162

163+
// TODO: split this to createFPExt/createFPTrunc when we have dedicated cast
164+
// operations.
165+
mlir::Value createFloatingCast(mlir::Value v, mlir::Type destType) {
166+
assert(!cir::MissingFeatures::fpConstraints());
167+
168+
return create<cir::CastOp>(v.getLoc(), destType, cir::CastKind::floating,
169+
v);
170+
}
171+
163172
mlir::Value createFSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
164173
assert(!cir::MissingFeatures::metaDataNode());
165174
assert(!cir::MissingFeatures::fpConstraints());

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,11 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
8888
//===--------------------------------------------------------------------===//
8989

9090
mlir::Value emitPromotedValue(mlir::Value result, QualType promotionType) {
91-
cgf.cgm.errorNYI(result.getLoc(), "floating cast for promoted value");
92-
return {};
91+
return builder.createFloatingCast(result, cgf.convertType(promotionType));
9392
}
9493

9594
mlir::Value emitUnPromotedValue(mlir::Value result, QualType exprType) {
96-
cgf.cgm.errorNYI(result.getLoc(), "floating cast for unpromoted value");
97-
return {};
95+
return builder.createFloatingCast(result, cgf.convertType(exprType));
9896
}
9997

10098
mlir::Value emitPromoted(const Expr *e, QualType promotionType);
@@ -446,37 +444,35 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
446444
llvm_unreachable("Unexpected signed overflow behavior kind");
447445
}
448446

449-
mlir::Value VisitUnaryPlus(const UnaryOperator *e,
450-
QualType promotionType = QualType()) {
451-
if (!promotionType.isNull())
452-
cgf.cgm.errorNYI(e->getSourceRange(), "VisitUnaryPlus: promotionType");
453-
assert(!cir::MissingFeatures::opUnaryPromotionType());
454-
mlir::Value result = emitUnaryPlusOrMinus(e, cir::UnaryOpKind::Plus);
455-
return result;
447+
mlir::Value VisitUnaryPlus(const UnaryOperator *e) {
448+
return emitUnaryPlusOrMinus(e, cir::UnaryOpKind::Plus);
456449
}
457450

458-
mlir::Value VisitUnaryMinus(const UnaryOperator *e,
459-
QualType promotionType = QualType()) {
460-
if (!promotionType.isNull())
461-
cgf.cgm.errorNYI(e->getSourceRange(), "VisitUnaryMinus: promotionType");
462-
assert(!cir::MissingFeatures::opUnaryPromotionType());
463-
mlir::Value result = emitUnaryPlusOrMinus(e, cir::UnaryOpKind::Minus);
464-
return result;
451+
mlir::Value VisitUnaryMinus(const UnaryOperator *e) {
452+
return emitUnaryPlusOrMinus(e, cir::UnaryOpKind::Minus);
465453
}
466454

467455
mlir::Value emitUnaryPlusOrMinus(const UnaryOperator *e,
468456
cir::UnaryOpKind kind) {
469457
ignoreResultAssign = false;
470458

471-
assert(!cir::MissingFeatures::opUnaryPromotionType());
472-
mlir::Value operand = Visit(e->getSubExpr());
459+
QualType promotionType = getPromotionType(e->getSubExpr()->getType());
460+
461+
mlir::Value operand;
462+
if (!promotionType.isNull())
463+
operand = cgf.emitPromotedScalarExpr(e->getSubExpr(), promotionType);
464+
else
465+
operand = Visit(e->getSubExpr());
473466

474467
bool nsw =
475468
kind == cir::UnaryOpKind::Minus && e->getType()->isSignedIntegerType();
476469

477470
// NOTE: LLVM codegen will lower this directly to either a FNeg
478471
// or a Sub instruction. In CIR this will be handled later in LowerToLLVM.
479-
return emitUnaryOp(e, kind, operand, nsw);
472+
mlir::Value result = emitUnaryOp(e, kind, operand, nsw);
473+
if (result && !promotionType.isNull())
474+
return emitUnPromotedValue(result, e->getType());
475+
return result;
480476
}
481477

482478
mlir::Value emitUnaryOp(const UnaryOperator *e, cir::UnaryOpKind kind,

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,19 +1896,6 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue,
18961896
lvalue.getTBAAInfo(), lvalue.isNontemporal());
18971897
}
18981898

1899-
static bool hasBooleanRepresentation(QualType Ty) {
1900-
if (Ty->isBooleanType())
1901-
return true;
1902-
1903-
if (const EnumType *ET = Ty->getAs<EnumType>())
1904-
return ET->getDecl()->getIntegerType()->isBooleanType();
1905-
1906-
if (const AtomicType *AT = Ty->getAs<AtomicType>())
1907-
return hasBooleanRepresentation(AT->getValueType());
1908-
1909-
return false;
1910-
}
1911-
19121899
static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
19131900
llvm::APInt &Min, llvm::APInt &End,
19141901
bool StrictEnums, bool IsBool) {
@@ -1931,7 +1918,7 @@ static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
19311918
llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
19321919
llvm::APInt Min, End;
19331920
if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums,
1934-
hasBooleanRepresentation(Ty)))
1921+
Ty->hasBooleanRepresentation()))
19351922
return nullptr;
19361923

19371924
llvm::MDBuilder MDHelper(getLLVMContext());
@@ -1945,7 +1932,7 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
19451932
if (!HasBoolCheck && !HasEnumCheck)
19461933
return false;
19471934

1948-
bool IsBool = hasBooleanRepresentation(Ty) ||
1935+
bool IsBool = Ty->hasBooleanRepresentation() ||
19491936
NSAPI(CGM.getContext()).isObjCBOOLType(Ty);
19501937
bool NeedsBoolCheck = HasBoolCheck && IsBool;
19511938
bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>();
@@ -2073,7 +2060,7 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile,
20732060
/// by ConvertType) to its load/store type (as returned by
20742061
/// convertTypeForLoadStore).
20752062
llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
2076-
if (hasBooleanRepresentation(Ty) || Ty->isBitIntType()) {
2063+
if (Ty->hasBooleanRepresentation() || Ty->isBitIntType()) {
20772064
llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType());
20782065
bool Signed = Ty->isSignedIntegerOrEnumerationType();
20792066
return Builder.CreateIntCast(Value, StoreTy, Signed, "storedv");
@@ -2114,7 +2101,7 @@ llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
21142101
}
21152102

21162103
llvm::Type *ResTy = ConvertType(Ty);
2117-
if (hasBooleanRepresentation(Ty) || Ty->isBitIntType() ||
2104+
if (Ty->hasBooleanRepresentation() || Ty->isBitIntType() ||
21182105
Ty->isExtVectorBoolType())
21192106
return Builder.CreateTrunc(Value, ResTy, "loadedv");
21202107

@@ -2601,7 +2588,7 @@ void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
26012588
Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load");
26022589

26032590
// Mask the source value as needed.
2604-
if (!hasBooleanRepresentation(Dst.getType()))
2591+
if (!Dst.getType()->hasBooleanRepresentation())
26052592
SrcVal = Builder.CreateAnd(
26062593
SrcVal, llvm::APInt::getLowBitsSet(StorageSize, Info.Size),
26072594
"bf.value");

clang/lib/CodeGen/CGHLSLBuiltins.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,19 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
380380
getDotProductIntrinsic(CGM.getHLSLRuntime(), VecTy0->getElementType()),
381381
ArrayRef<Value *>{Op0, Op1}, nullptr, "hlsl.dot");
382382
}
383+
case Builtin::BI__builtin_hlsl_dot2add: {
384+
llvm::Triple::ArchType Arch = CGM.getTarget().getTriple().getArch();
385+
assert(Arch == llvm::Triple::dxil &&
386+
"Intrinsic dot2add is only allowed for dxil architecture");
387+
Value *A = EmitScalarExpr(E->getArg(0));
388+
Value *B = EmitScalarExpr(E->getArg(1));
389+
Value *C = EmitScalarExpr(E->getArg(2));
390+
391+
Intrinsic::ID ID = llvm ::Intrinsic::dx_dot2add;
392+
return Builder.CreateIntrinsic(
393+
/*ReturnType=*/C->getType(), ID, ArrayRef<Value *>{A, B, C}, nullptr,
394+
"dx.dot2add");
395+
}
383396
case Builtin::BI__builtin_hlsl_dot4add_i8packed: {
384397
Value *A = EmitScalarExpr(E->getArg(0));
385398
Value *B = EmitScalarExpr(E->getArg(1));

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,8 @@ struct DarwinPlatform {
18861886
assert(IsValid && "invalid SDK version");
18871887
return DarwinSDKInfo(
18881888
Version,
1889-
/*MaximumDeploymentTarget=*/VersionTuple(Version.getMajor(), 0, 99));
1889+
/*MaximumDeploymentTarget=*/VersionTuple(Version.getMajor(), 0, 99),
1890+
getOSFromPlatform(Platform));
18901891
}
18911892

18921893
private:
@@ -1916,6 +1917,23 @@ struct DarwinPlatform {
19161917
}
19171918
}
19181919

1920+
static llvm::Triple::OSType getOSFromPlatform(DarwinPlatformKind Platform) {
1921+
switch (Platform) {
1922+
case DarwinPlatformKind::MacOS:
1923+
return llvm::Triple::MacOSX;
1924+
case DarwinPlatformKind::IPhoneOS:
1925+
return llvm::Triple::IOS;
1926+
case DarwinPlatformKind::TvOS:
1927+
return llvm::Triple::TvOS;
1928+
case DarwinPlatformKind::WatchOS:
1929+
return llvm::Triple::WatchOS;
1930+
case DarwinPlatformKind::DriverKit:
1931+
return llvm::Triple::DriverKit;
1932+
case DarwinPlatformKind::XROS:
1933+
return llvm::Triple::XROS;
1934+
}
1935+
}
1936+
19191937
SourceKind Kind;
19201938
DarwinPlatformKind Platform;
19211939
DarwinEnvironmentKind Environment = DarwinEnvironmentKind::NativeEnvironment;
@@ -2966,20 +2984,8 @@ bool Darwin::isAlignedAllocationUnavailable() const {
29662984
return TargetVersion < alignedAllocMinVersion(OS);
29672985
}
29682986

2969-
static bool sdkSupportsBuiltinModules(
2970-
const Darwin::DarwinPlatformKind &TargetPlatform,
2971-
const Darwin::DarwinEnvironmentKind &TargetEnvironment,
2972-
const std::optional<DarwinSDKInfo> &SDKInfo) {
2973-
if (TargetEnvironment == Darwin::NativeEnvironment ||
2974-
TargetEnvironment == Darwin::Simulator ||
2975-
TargetEnvironment == Darwin::MacCatalyst) {
2976-
// Standard xnu/Mach/Darwin based environments
2977-
// depend on the SDK version.
2978-
} else {
2979-
// All other environments support builtin modules from the start.
2980-
return true;
2981-
}
2982-
2987+
static bool
2988+
sdkSupportsBuiltinModules(const std::optional<DarwinSDKInfo> &SDKInfo) {
29832989
if (!SDKInfo)
29842990
// If there is no SDK info, assume this is building against a
29852991
// pre-SDK version of macOS (i.e. before Mac OS X 10.4). Those
@@ -2990,26 +2996,18 @@ static bool sdkSupportsBuiltinModules(
29902996
return false;
29912997

29922998
VersionTuple SDKVersion = SDKInfo->getVersion();
2993-
switch (TargetPlatform) {
2999+
switch (SDKInfo->getOS()) {
29943000
// Existing SDKs added support for builtin modules in the fall
29953001
// 2024 major releases.
2996-
case Darwin::MacOS:
3002+
case llvm::Triple::MacOSX:
29973003
return SDKVersion >= VersionTuple(15U);
2998-
case Darwin::IPhoneOS:
2999-
switch (TargetEnvironment) {
3000-
case Darwin::MacCatalyst:
3001-
// Mac Catalyst uses `-target arm64-apple-ios18.0-macabi` so the platform
3002-
// is iOS, but it builds with the macOS SDK, so it's the macOS SDK version
3003-
// that's relevant.
3004-
return SDKVersion >= VersionTuple(15U);
3005-
default:
3006-
return SDKVersion >= VersionTuple(18U);
3007-
}
3008-
case Darwin::TvOS:
3004+
case llvm::Triple::IOS:
30093005
return SDKVersion >= VersionTuple(18U);
3010-
case Darwin::WatchOS:
3006+
case llvm::Triple::TvOS:
3007+
return SDKVersion >= VersionTuple(18U);
3008+
case llvm::Triple::WatchOS:
30113009
return SDKVersion >= VersionTuple(11U);
3012-
case Darwin::XROS:
3010+
case llvm::Triple::XROS:
30133011
return SDKVersion >= VersionTuple(2U);
30143012

30153013
// New SDKs support builtin modules from the start.
@@ -3138,7 +3136,7 @@ void Darwin::addClangTargetOptions(
31383136
// i.e. when the builtin stdint.h is in the Darwin module too, the cycle
31393137
// goes away. Note that -fbuiltin-headers-in-system-modules does nothing
31403138
// to fix the same problem with C++ headers, and is generally fragile.
3141-
if (!sdkSupportsBuiltinModules(TargetPlatform, TargetEnvironment, SDKInfo))
3139+
if (!sdkSupportsBuiltinModules(SDKInfo))
31423140
CC1Args.push_back("-fbuiltin-headers-in-system-modules");
31433141

31443142
if (!DriverArgs.hasArgNoClaim(options::OPT_fdefine_target_os_macros,

0 commit comments

Comments
 (0)