Skip to content

Commit ae9b72d

Browse files
Merge branch 'master' into fix-hoisting-issue
2 parents 54b1105 + 4d286aa commit ae9b72d

Some content is hidden

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

50 files changed

+921
-199
lines changed

cmake/CompilerFlags.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ function(set_default_compile_options target)
205205
$<$<BOOL:${SLANG_ENABLE_FULL_DEBUG_VALIDATION}>:SLANG_ENABLE_FULL_IR_VALIDATION>
206206
$<$<BOOL:${SLANG_ENABLE_IR_BREAK_ALLOC}>:SLANG_ENABLE_IR_BREAK_ALLOC>
207207
$<$<BOOL:${SLANG_ENABLE_DX_ON_VK}>:SLANG_CONFIG_DX_ON_VK>
208+
$<$<STREQUAL:${SLANG_LIB_TYPE},STATIC>:STB_IMAGE_STATIC>
208209
)
209210

210211
if(SLANG_ENABLE_ASAN)

docs/building.md

+15
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,21 @@ cmake -B build -G Ninja
321321
cmake --build build -j
322322
```
323323

324+
## Static linking against libslang
325+
326+
If linking against a static `libslang.a` you will need to link against some
327+
dependencies also if you're not already incorporating them into your project.
328+
329+
You will need to link against:
330+
331+
```
332+
${SLANG_DIR}/build/Release/lib/libslang.a
333+
${SLANG_DIR}/build/Release/lib/libcompiler-core.a
334+
${SLANG_DIR}/build/Release/lib/libcore.a
335+
${SLANG_DIR}/build/external/miniz/libminiz.a
336+
${SLANG_DIR}/build/external/lz4/build/cmake/liblz4.a
337+
```
338+
324339
## Notes
325340

326341
[^1] below 3.25, CMake lacks the ability to mark directories as being

external/CMakeLists.txt

+10-2
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,19 @@ endif()
8989

9090
# imgui
9191
add_library(imgui INTERFACE)
92-
target_include_directories(imgui INTERFACE "${CMAKE_CURRENT_LIST_DIR}/imgui")
92+
target_include_directories(
93+
imgui
94+
${system}
95+
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/imgui"
96+
)
9397

9498
# stb
9599
add_library(stb INTERFACE)
96-
target_include_directories(stb INTERFACE "${CMAKE_CURRENT_LIST_DIR}/stb")
100+
target_include_directories(
101+
stb
102+
${system}
103+
INTERFACE "${CMAKE_CURRENT_LIST_DIR}/stb"
104+
)
97105

98106
# slang-rhi
99107
if(SLANG_ENABLE_SLANG_RHI)

source/slang/core.meta.slang

+24-69
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//public module core;
1+
public module core;
22

33
// Slang `core` library
44

@@ -2367,49 +2367,6 @@ __generic<T> __extension vector<T, 4>
23672367

23682368
${{{{
23692369

2370-
// The above extensions are generic in the *type* of the vector,
2371-
// but explicit in the *size*. We will now declare an extension
2372-
// for each builtin type that is generic in the size.
2373-
//
2374-
for (int tt = 0; tt < kBaseTypeCount; ++tt)
2375-
{
2376-
if(kBaseTypes[tt].tag == BaseType::Void) continue;
2377-
2378-
sb << "__generic<let N : int> __extension vector<"
2379-
<< kBaseTypes[tt].name << ",N>\n{\n";
2380-
2381-
for (int ff = 0; ff < kBaseTypeCount; ++ff)
2382-
{
2383-
if(kBaseTypes[ff].tag == BaseType::Void) continue;
2384-
2385-
2386-
if( tt != ff )
2387-
{
2388-
auto cost = getBaseTypeConversionCost(
2389-
kBaseTypes[tt],
2390-
kBaseTypes[ff]);
2391-
auto op = getBaseTypeConversionOp(
2392-
kBaseTypes[tt],
2393-
kBaseTypes[ff]);
2394-
2395-
// Implicit conversion from a vector of the same
2396-
// size, but different element type.
2397-
sb << " __implicit_conversion(" << cost << ")\n";
2398-
sb << " __intrinsic_op(" << int(op) << ")\n";
2399-
sb << " __init(vector<" << kBaseTypes[ff].name << ",N> value);\n";
2400-
2401-
// Constructor to make a vector from a scalar of another type.
2402-
if (cost != kConversionCost_Impossible)
2403-
{
2404-
cost += kConversionCost_ScalarToVector;
2405-
sb << " __implicit_conversion(" << cost << ")\n";
2406-
sb << " [__unsafeForceInlineEarly]\n";
2407-
sb << " __init(" << kBaseTypes[ff].name << " value) { this = vector<" << kBaseTypes[tt].name << ",N>( " << kBaseTypes[tt].name << "(value)); }\n";
2408-
}
2409-
}
2410-
}
2411-
sb << "}\n";
2412-
}
24132370

24142371
for( int R = 1; R <= 4; ++R )
24152372
for( int C = 1; C <= 4; ++C )
@@ -2464,38 +2421,36 @@ for( int C = 1; C <= 4; ++C )
24642421
sb << "}\n";
24652422
}
24662423

2467-
for (int tt = 0; tt < kBaseTypeCount; ++tt)
2468-
{
2469-
if(kBaseTypes[tt].tag == BaseType::Void) continue;
2470-
auto toType = kBaseTypes[tt].name;
24712424
}}}}
24722425

2473-
__generic<let R : int, let C : int, let L : int> extension matrix<$(toType),R,C,L>
2426+
//@hidden:
2427+
__intrinsic_op($(kIROp_BuiltinCast))
2428+
internal T __builtin_cast<T, U>(U u);
2429+
2430+
// If T is implicitly convertible to U, then vector<T,N> is implicitly convertible to vector<U,N>.
2431+
__generic<ToType, let N : int> extension vector<ToType,N>
24742432
{
2475-
${{{{
2476-
for (int ff = 0; ff < kBaseTypeCount; ++ff)
2477-
{
2478-
if(kBaseTypes[ff].tag == BaseType::Void) continue;
2479-
if( tt == ff ) continue;
2433+
__implicit_conversion(constraint)
2434+
__intrinsic_op(BuiltinCast)
2435+
__init<FromType>(vector<FromType,N> value) where ToType(FromType) implicit;
24802436

2481-
auto cost = getBaseTypeConversionCost(
2482-
kBaseTypes[tt],
2483-
kBaseTypes[ff]);
2484-
auto fromType = kBaseTypes[ff].name;
2485-
auto op = getBaseTypeConversionOp(
2486-
kBaseTypes[tt],
2487-
kBaseTypes[ff]);
2488-
}}}}
2489-
__implicit_conversion($(cost))
2490-
__intrinsic_op($(op))
2491-
__init(matrix<$(fromType),R,C,L> value);
2492-
${{{{
2437+
__implicit_conversion(constraint+)
2438+
[__unsafeForceInlineEarly]
2439+
[__readNone]
2440+
[TreatAsDifferentiable]
2441+
__init<FromType>(FromType value) where ToType(FromType) implicit
2442+
{
2443+
this = __builtin_cast<vector<ToType,N>>(vector<FromType,N>(value));
24932444
}
2494-
}}}}
24952445
}
2496-
${{{{
2446+
2447+
// If T is implicitly convertible to U, then matrix<T,R,C,L> is implicitly convertible to matrix<U,R,C,L>.
2448+
__generic<ToType, let R : int, let C : int, let L : int> extension matrix<ToType,R,C,L>
2449+
{
2450+
__implicit_conversion(constraint)
2451+
__intrinsic_op(BuiltinCast)
2452+
__init<FromType>(matrix<FromType,R,C,L> value) where ToType(FromType) implicit;
24972453
}
2498-
}}}}
24992454

25002455
//@ hidden:
25012456
__generic<T, U>

source/slang/diff.meta.slang

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/// `[ForwardDerivative(fwdFn)]` attribute can be used to provide a forward-mode
32
/// derivative implementation.
43
/// Invoking `fwd_diff(decoratedFn)` will place a call to `fwdFn` instead of synthesizing
@@ -80,7 +79,6 @@ attribute_syntax [ForwardDerivative(function)] : ForwardDerivativeAttribute;
8079
/// For member functions, or functions nested inside namespaces, `bwdFn` may need to be a fully qualified
8180
/// name.
8281
///
83-
///
8482
__attributeTarget(FunctionDeclBase)
8583
attribute_syntax [BackwardDerivative(function)] : BackwardDerivativeAttribute;
8684

@@ -2150,17 +2148,19 @@ DifferentialPair<T> __d_clamp(DifferentialPair<T> dpx, DifferentialPair<T> dpMin
21502148
{
21512149
return DifferentialPair<T>(
21522150
clamp(dpx.p, dpMin.p, dpMax.p),
2153-
dpx.p < dpMin.p ? dpMin.d : (dpx.p > dpMax.p ? dpMax.d : dpx.d));
2151+
(dpx.p >= dpMin.p && dpx.p <= dpMax.p) ? dpx.d : (dpx.p < dpMin.p ? dpMin.d : dpMax.d));
21542152
}
21552153
__generic<T : __BuiltinFloatingPointType>
21562154
[BackwardDifferentiable]
21572155
[PreferRecompute]
21582156
[BackwardDerivativeOf(clamp)]
21592157
void __d_clamp(inout DifferentialPair<T> dpx, inout DifferentialPair<T> dpMin, inout DifferentialPair<T> dpMax, T.Differential dOut)
21602158
{
2161-
dpx = diffPair(dpx.p, dpx.p > dpMin.p && dpx.p < dpMax.p ? dOut : T.dzero());
2162-
dpMin = diffPair(dpMin.p, dpx.p <= dpMin.p ? dOut : T.dzero());
2163-
dpMax = diffPair(dpMax.p, dpx.p >= dpMax.p ? dOut : T.dzero());
2159+
// Propagate the derivative to x if x is within [min, max] (including the boundaries).
2160+
dpx = diffPair(dpx.p, (dpx.p >= dpMin.p && dpx.p <= dpMax.p) ? dOut : T.dzero());
2161+
// If x is strictly below min or above max, the gradient is instead applied to the clamp bounds
2162+
dpMin = diffPair(dpMin.p, dpx.p < dpMin.p ? dOut : T.dzero());
2163+
dpMax = diffPair(dpMax.p, dpx.p > dpMax.p ? dOut : T.dzero());
21642164
}
21652165
VECTOR_MATRIX_TERNARY_DIFF_IMPL(clamp)
21662166

source/slang/hlsl.meta.slang

+4
Original file line numberDiff line numberDiff line change
@@ -23667,6 +23667,8 @@ void coopVecOuterProductAccumulate<T : __BuiltinArithmeticType, let M : int, let
2366723667
{
2366823668
__target_switch
2366923669
{
23670+
case hlsl:
23671+
__intrinsic_asm "$0.OuterProductAccumulate($1, $2, $3, $4, $5, $6)";
2367023672
case spirv:
2367123673
let matrixInterpretationSpirv : int = __getSpvCoopVecComponentType(matrixInterpretation);
2367223674
let memoryLayoutSpirv : int = __getSpvCoopVecMatrixLayout(memoryLayout);
@@ -23742,6 +23744,8 @@ void coopVecReduceSumAccumulate<T : __BuiltinArithmeticType, let N : int>(
2374223744
{
2374323745
__target_switch
2374423746
{
23747+
case hlsl:
23748+
__intrinsic_asm "$0.ReduceSumAccumulate($1, $2)";
2374523749
case spirv:
2374623750
let bufferPtr = buffer.GetBufferPointer();
2374723751
spirv_asm

source/slang/slang-ast-builder.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,14 @@ SubtypeWitness* ASTBuilder::getConjunctionSubtypeWitness(
948948
return witness;
949949
}
950950

951+
TypeCoercionWitness* ASTBuilder::getTypeCoercionWitness(
952+
Type* subType,
953+
Type* superType,
954+
DeclRef<Decl> declRef)
955+
{
956+
return getOrCreate<TypeCoercionWitness>(subType, superType, declRef.declRefBase);
957+
}
958+
951959
DeclRef<Decl> _getMemberDeclRef(ASTBuilder* builder, DeclRef<Decl> parent, Decl* decl)
952960
{
953961
return builder->getMemberDeclRef(parent, decl);

source/slang/slang-ast-builder.h

+5
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@ class ASTBuilder : public RefObject
636636
SubtypeWitness* subIsLWitness,
637637
SubtypeWitness* subIsRWitness);
638638

639+
TypeCoercionWitness* getTypeCoercionWitness(
640+
Type* fromType,
641+
Type* toType,
642+
DeclRef<Decl> declRef);
643+
639644
/// Helpers to get type info from the SharedASTBuilder
640645
const ReflectClassInfo* findClassInfo(const UnownedStringSlice& slice)
641646
{

source/slang/slang-ast-decl.h

+9
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,15 @@ class GenericTypeConstraintDecl : public TypeConstraintDecl
612612
const TypeExp& _getSupOverride() const { return sup; }
613613
};
614614

615+
class TypeCoercionConstraintDecl : public Decl
616+
{
617+
SLANG_AST_CLASS(TypeCoercionConstraintDecl)
618+
619+
SourceLoc whereTokenLoc = SourceLoc();
620+
TypeExp fromType;
621+
TypeExp toType;
622+
};
623+
615624
class GenericValueParamDecl : public VarDeclBase
616625
{
617626
SLANG_AST_CLASS(GenericValueParamDecl)

source/slang/slang-ast-modifier.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1279,10 +1279,10 @@ class ImplicitConversionModifier : public Modifier
12791279
SLANG_AST_CLASS(ImplicitConversionModifier)
12801280

12811281
// The conversion cost, used to rank conversions
1282-
ConversionCost cost;
1282+
ConversionCost cost = kConversionCost_None;
12831283

12841284
// A builtin identifier for identifying conversions that need special treatment.
1285-
BuiltinConversionKind builtinConversionKind;
1285+
BuiltinConversionKind builtinConversionKind = kBuiltinConversion_Unknown;
12861286
};
12871287

12881288
class FormatAttribute : public Attribute

source/slang/slang-ast-support-types.h

+5
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ enum : ConversionCost
178178
// Additional cost when casting an LValue.
179179
kConversionCost_LValueCast = 800,
180180

181+
// The cost of this conversion is defined by the type coercion constraint.
182+
kConversionCost_TypeCoercionConstraint = 1000,
183+
kConversionCost_TypeCoercionConstraintPlusScalarToVector =
184+
kConversionCost_TypeCoercionConstraint + kConversionCost_ScalarToVector,
185+
181186
// Conversion is impossible
182187
kConversionCost_Impossible = 0xFFFFFFFF,
183188
};

source/slang/slang-ast-val.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,58 @@ void ExtractFromConjunctionSubtypeWitness::_toTextOverride(StringBuilder& out)
845845
out << ")";
846846
}
847847

848+
void TypeCoercionWitness::_toTextOverride(StringBuilder& out)
849+
{
850+
out << "TypeCoercionWitness(";
851+
if (getFromType())
852+
out << getFromType();
853+
if (getToType())
854+
out << getToType();
855+
out << ")";
856+
}
857+
858+
Val* TypeCoercionWitness::_substituteImplOverride(
859+
ASTBuilder* astBuilder,
860+
SubstitutionSet subst,
861+
int* ioDiff)
862+
{
863+
int diff = 0;
864+
865+
auto substDeclRef = getDeclRef().substituteImpl(astBuilder, subst, &diff);
866+
auto substFrom = as<Type>(getFromType()->substituteImpl(astBuilder, subst, &diff));
867+
auto substTo = as<Type>(getToType()->substituteImpl(astBuilder, subst, &diff));
868+
869+
if (!diff)
870+
return this;
871+
872+
(*ioDiff)++;
873+
874+
TypeCoercionWitness* substValue =
875+
astBuilder->getTypeCoercionWitness(substFrom, substTo, substDeclRef);
876+
return substValue;
877+
}
878+
879+
Val* TypeCoercionWitness::_resolveImplOverride()
880+
{
881+
Val* resolvedDeclRef = nullptr;
882+
if (getDeclRef())
883+
resolvedDeclRef = getDeclRef().declRefBase->resolve();
884+
if (auto resolvedVal = as<Witness>(resolvedDeclRef))
885+
return resolvedVal;
886+
887+
auto newFrom = as<Type>(getFromType()->resolve());
888+
auto newTo = as<Type>(getToType()->resolve());
889+
890+
auto newDeclRef = as<DeclRefBase>(resolvedDeclRef);
891+
if (!newDeclRef)
892+
newDeclRef = getDeclRef().declRefBase;
893+
if (newFrom != getFromType() || newTo != getToType() || newDeclRef != getDeclRef())
894+
{
895+
return getCurrentASTBuilder()->getTypeCoercionWitness(newFrom, newTo, newDeclRef);
896+
}
897+
return this;
898+
}
899+
848900
// UNormModifierVal
849901

850902
void UNormModifierVal::_toTextOverride(StringBuilder& out)

source/slang/slang-ast-val.h

+14
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,20 @@ class TypeEqualityWitness : public SubtypeWitness
621621
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
622622
};
623623

624+
class TypeCoercionWitness : public Witness
625+
{
626+
SLANG_AST_CLASS(TypeCoercionWitness)
627+
628+
Type* getFromType() { return as<Type>(getOperand(0)); }
629+
Type* getToType() { return as<Type>(getOperand(1)); }
630+
631+
DeclRef<Decl> getDeclRef() { return as<DeclRefBase>(getOperand(2)); }
632+
633+
void _toTextOverride(StringBuilder& out);
634+
Val* _substituteImplOverride(ASTBuilder* astBuilder, SubstitutionSet subst, int* ioDiff);
635+
Val* _resolveImplOverride();
636+
};
637+
624638
// A witness that one type is a subtype of another
625639
// because some in-scope declaration says so
626640
class DeclaredSubtypeWitness : public SubtypeWitness

0 commit comments

Comments
 (0)