summaryrefslogtreecommitdiff
path: root/source/slang/slang-check-expr.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2022-08-22 09:43:05 -0700
committerGitHub <noreply@github.com>2022-08-22 09:43:05 -0700
commit393185196ed65a9eeaf9502edbf3dcce87337d81 (patch)
tree91c9fa14ddb21d15e6cedf83f7aa6b649e99db86 /source/slang/slang-check-expr.cpp
parent15055d20c143cb398bd3e269541eebf24777390a (diff)
Support compile-time constant int val in the form of polynomials. (#2372)
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'source/slang/slang-check-expr.cpp')
-rw-r--r--source/slang/slang-check-expr.cpp47
1 files changed, 44 insertions, 3 deletions
diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp
index c96db6f3b..7257790af 100644
--- a/source/slang/slang-check-expr.cpp
+++ b/source/slang/slang-check-expr.cpp
@@ -928,7 +928,7 @@ namespace Slang
if (!allConst)
{
- // TODO(tfoley): We probably want to support a very limited number of operations
+ // We support a very limited number of operations
// on "constants" that aren't actually known, to be able to handle a generic
// that takes an integer `N` but then constructs a vector of size `N+1`.
//
@@ -937,7 +937,46 @@ namespace Slang
// need inference to be smart enough to know that `2 + N` and `N + 2` are the
// same value, as are `N + M + 1 + 1` and `M + 2 + N`.
//
- // For now we can just bail in this case.
+ // This is done by constructing a 'PolynomialIntVal' and rely on its
+ // `canonicalize` operation.
+ if (implicitCast)
+ {
+ // We cannot support casting in this case.
+ return nullptr;
+ }
+
+ auto opName = funcDeclRef.getName();
+
+ // handle binary operators
+ if (opName == getName("-"))
+ {
+ if (argCount == 1)
+ {
+ return PolynomialIntVal::neg(m_astBuilder, argVals[0]);
+ }
+ else if (argCount == 2)
+ {
+ return PolynomialIntVal::sub(m_astBuilder, argVals[0], argVals[1]);
+ }
+ }
+ else if (opName == getName("+"))
+ {
+ if (argCount == 1)
+ {
+ return argVals[0];
+ }
+ else if (argCount == 2)
+ {
+ return PolynomialIntVal::add(m_astBuilder, argVals[0], argVals[1]);
+ }
+ }
+ else if (opName == getName("*"))
+ {
+ if (argCount == 2)
+ {
+ return PolynomialIntVal::mul(m_astBuilder, argVals[0], argVals[1]);
+ }
+ }
return nullptr;
}
@@ -1110,7 +1149,9 @@ namespace Slang
if (auto genericValParamRef = declRef.as<GenericValueParamDecl>())
{
// TODO(tfoley): handle the case of non-`int` value parameters...
- return m_astBuilder->create<GenericParamIntVal>(genericValParamRef);
+ Val* valResult = m_astBuilder->create<GenericParamIntVal>(genericValParamRef);
+ valResult = valResult->substitute(m_astBuilder, expr.getSubsts());
+ return as<IntVal>(valResult);
}
// We may also need to check for references to variables that