In CSL, it is possible to ensure that code is executed at compile-time by using theDocumentation Index
Fetch the complete documentation index at: https://sdk.cerebras.ai/llms.txt
Use this file to discover all available pages before exploring further.
comptime keyword, which guarantees that the code will have no
run-time footprint. An error is emitted if compile-time evaluation is not
possible.
Comptime Variables
Acomptime variable guarantees that all loads and stores to this variable
happen at compile-time. As such, this variable has no run-time footprint and
its address cannot be obtained. Unlike constants, comptime variables can be
modified, as shown below.
comptime variables need to be declared inside a block or a function. For
global variables, using const is enough: the initializer of global variables
is always implicitly comptime, since CSL does not support run-time
initialization of global variables.
Since all loads and stores to a comptime variable must happen at
compile-time, the stored value must be comptime-known (explained below) and any
offsets (like array indices) must be comptime-known as well. Similarly, stores
to comptime variables must not depend on run-time control flow.
Comptime-Known Values
All values that are known to the compiler at compile-time are comptime-known values. Formally, the compiler uses the following rules to determine whether a value is comptime-known:- All literals (e.g.
1.0) are comptime-known values. - All
constvariables with a comptime-known initializer (e.g.const x = 1.0) are comptime-known values. - All uses of
comptimeorparamvariables are comptime-known values. - Expressions comprised of two or more comptime-known values (e.g.
1.0 + 2.0 * x) are comptime-known values. However, function calls are an exception to this rule.
Comptime Expressions
Expressions that depend on constants or othercomptime variables can be
explicitly marked for evaluation at compile time by prefixing the expression
with the comptime keyword. For instance, the following snippet ensures that
the call to foo() is replaced with its return value at compile time, so that
we do not pay a run-time cost for the function call.
and and
or operators) will short-circuit, if possible, even at comptime. When
short-circuiting applies, semantic checks like type-checking and checks
for unbound identifiers will not be applied to the right-hand operand, as
shown in the example below:
Types Whose Values are Required to be Comptime
Certain types are only allowed to exist in expressions and variables that arecomptime. In general, these refer to types whose values:
- have no possible memory layout associated with them, or
- are required to be comptime to enable compiler analyses.
comptime.
If any of these types is used as the type of a variable, these variables must
be comptime var or const with a comptime initializer.
Pointers to these types are not allowed.
Values of the following types must always be comptime-known:
comptime_intcomptime_floattypecomptime_string- function type
imported_module
Evaluation of Comptime-Known Control Flow
If the predicate of anif statement is a comptime-known value, the if
statement is replaced with the block corresponding to the branch taken (if any),
no run-time branches are created, and the block corresponding to the branch not
taken is not semantically checked, as illustrated below:
comptime loops, all expressions and variables are implicitly
comptime. This includes the induction variables of for loops and the
continue expression of while loops.
Typical Uses of the comptime Keyword
comptime variables and operations enable powerful operations such as
non-trivial memory initialization or routing rules, without paying a performance
penalty at run-time. For instance, the following code initializes a global
array as an identity matrix at compile time.
createIdentityMatrix(),
since that function is called on the host during program compilation.
