Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
ca7c920
handle `nnkSym` in `getInnerPointerType`
Vindaar Aug 11, 2025
561328e
fix `constructPtrSignature` after change from nil -> gtVoid for iTyp
Vindaar Aug 11, 2025
d7de955
handle `gpuCast` in determineSymKind/Mutability/Ident
Vindaar Aug 11, 2025
4277fcf
allow determination of GPU type to fail in `nimToGpuType`
Vindaar Aug 11, 2025
ba89dcb
handle `UncheckedArray` in `gpuTypeToString` on CUDA backend
Vindaar Aug 11, 2025
a2bbf98
rename constant to set workgroup size
Vindaar Aug 11, 2025
1c5ff0c
minor cleanup
Vindaar Aug 11, 2025
b98f069
correctly handle `gtUA` (UncheckedArray) on CUDA backend
Vindaar Aug 11, 2025
c0e1eb8
fix access of type for left ident in assignment
Vindaar Aug 12, 2025
24afcfe
rewrite compound assignment operators in all functions
Vindaar Aug 12, 2025
18d582f
extend `GpuFieldInit` by a type field
Vindaar Aug 14, 2025
fbfeab6
make sure to update symbols in global functions too
Vindaar Aug 14, 2025
1f04a82
remove local `determineIdent` from `genWebGpu`
Vindaar Aug 14, 2025
ebcff83
fix tree representation of GpuAst
Vindaar Aug 14, 2025
3a32d2b
add `mpairs`, `pairs` iterator for `GpuAst`
Vindaar Aug 14, 2025
a6186ae
support default initialization in obj constr fields
Vindaar Aug 14, 2025
f485deb
correctly handle `var foo {.constant.}` variables by ⇒ globals
Vindaar Aug 14, 2025
08e43c5
move rewriting of compound assignment out of `storagePass`
Vindaar Aug 14, 2025
00c4b16
implement lifting of struct pointer fields
Vindaar Aug 14, 2025
4046396
add helper to check code for validity
Vindaar Aug 14, 2025
f27b0a1
add support for full Nim generics in the context of GPU code
Vindaar Aug 14, 2025
d3e0c45
implement support for type aliases
Vindaar Aug 17, 2025
0d11103
allow "pulling in" procs from outside `cuda` scope
Vindaar Aug 17, 2025
2c3f8f6
add pretty printer for GpuType
Vindaar Aug 18, 2025
0bf2807
store all types in `ctx.types` table, support 'pulling' types...
Vindaar Aug 18, 2025
d1a7390
support generic instantiations, producing unique WGSL types
Vindaar Aug 18, 2025
3482a45
add `{.builtin.}` pragma intended to specify builtin procs in CUDA/WGSL
Vindaar Aug 18, 2025
cd20189
fix context version for `cuModuleGetGlobal`
Vindaar Aug 18, 2025
dff7987
allow to compile with `-d:debugCuda` to compile in debug mode
Vindaar Aug 20, 2025
b834dc9
ignore `varargs` pragma in procs
Vindaar Aug 20, 2025
9fb4900
make sure statically sized arrays are copied
Vindaar Aug 20, 2025
cbbc415
make sure to catch `CUdeviceptr` arguments
Vindaar Aug 20, 2025
d44da46
change CUDA codegen to similar style as WGSL
Vindaar Aug 21, 2025
5e383fd
improve error message for `Dot` node if not ident/deref in WGSL
Vindaar Aug 21, 2025
7eaa65f
always also inject `num_workgroups` argument into WGSL globals
Vindaar Aug 21, 2025
151e025
rename WGSL `storagePass` to `preprocess`
Vindaar Aug 21, 2025
9874829
remove `gpuTypeToString`, `genFunctionType` from `backends`
Vindaar Aug 21, 2025
c617379
[CUDA] fix indentation for variable declarations
Vindaar Aug 22, 2025
25c5b13
improve type deduction for types that look generic
Vindaar Aug 22, 2025
ed5efd2
clone `forwardDeclare` in GpuAst
Vindaar Aug 22, 2025
d1cefd1
support tuples by mapping to object types
Vindaar Aug 22, 2025
58d4608
support Nim's implicit `result` variable
Vindaar Aug 22, 2025
9d5b02d
first steps towards supporting expressions
Vindaar Aug 22, 2025
cc1eaf7
move `cIsExpr` in clone to correct branch
Vindaar Aug 22, 2025
7e43f71
also catch tuple types from `nnkBracketExpr`
Vindaar Aug 22, 2025
39c4158
handle `inline` pragma same as `forceinline`
Vindaar Aug 22, 2025
bce536f
fix detection of custom `result` by looking into `gpuBlock`
Vindaar Aug 22, 2025
dfd4f56
fix handling of `skipSemicolon` for nested usage
Vindaar Aug 25, 2025
8cdf72c
if last expression _is_ return, we don't need `result` variable
Vindaar Aug 25, 2025
a40a87f
remove debug output
Vindaar Aug 25, 2025
396de7b
doc comment updates for procs in gpu_field_ops
Vindaar Aug 25, 2025
cd68552
do not emit semicolon in binary operand child nodes
Vindaar Aug 25, 2025
30a4ac4
handle function overloads by using `iSym` if encountered
Vindaar Aug 25, 2025
a0daf16
ignore `magic` pragma procs
Vindaar Aug 25, 2025
a260bac
ignore forward declarations in Nim -> GpuAst
Vindaar Aug 25, 2025
e0a00ee
add BigInt comparison and `toCanonical` conversion
Vindaar Aug 26, 2025
c9d9902
add FIPS Montgomery multiplication for fields without spare bits
Vindaar Aug 26, 2025
b6a63ef
[cuda] for now emit `constexpr` for a `gpuConstexpr` (i.e. Nim `const`)
Vindaar Aug 26, 2025
cf6fcbf
[cuda] handle generic instantiations and `UncheckedArray`
Vindaar Aug 28, 2025
1e8f84d
[wgsl] do not suffix generic inst types if they have no args
Vindaar Aug 28, 2025
e6cab34
handle `raises`, `noinit` pragmas
Vindaar Aug 28, 2025
91a87e7
use `getTypeName` for tuple type fields
Vindaar Aug 28, 2025
bb4e8fb
map `ntyString` to string explicitly in gpu type kinds
Vindaar Aug 28, 2025
de2cf85
map ntyUnused2 (lent T) to ptr T
Vindaar Aug 28, 2025
1b0aa55
generate type names for bracket expressions e.g. generics
Vindaar Aug 28, 2025
1a4dc31
improve generic inst -> gpu type by handling nnkSym
Vindaar Aug 28, 2025
8583d23
overwrite function names of problematic identifiers
Vindaar Aug 28, 2025
bf27dcb
refactor proc signature parsing & unify adding types to `ctx.types`
Vindaar Aug 28, 2025
81bd6f6
handle recursive calls in GPU code
Vindaar Aug 28, 2025
6cd1a5e
fix `modadd` in debug builds on CUDA
Vindaar Aug 29, 2025
8ae1427
fix reassignment of `types` in RT codegen for aliases
Vindaar Sep 1, 2025
ac91c05
handle `nnkObjConstr` correctly when encountering gtGenericInst
Vindaar Sep 1, 2025
04ddb39
do not emit `f` suffix for float literals
Vindaar Sep 2, 2025
7b51b65
[wgsl] append `;` for aliases
Vindaar Sep 2, 2025
2d5b367
rewrite infix as `gpuCall` if arguments not basic types
Vindaar Sep 2, 2025
c779258
support arbitrary values in array literals
Vindaar Sep 2, 2025
e395ecd
handle generic instantiation with actual initializations
Vindaar Sep 2, 2025
ea2b36e
fix top level type definitions
Vindaar Sep 2, 2025
96508f6
[cuda] add pass to strip `deref` if found inside `index` for pointers
Vindaar Sep 3, 2025
c823f9f
remove old comment
Vindaar Sep 3, 2025
6527759
better handle replacement of derefs
Vindaar Sep 3, 2025
3095d4f
remove Nim gensym'd suffix from `tmpTuple` variables
Vindaar Sep 3, 2025
59c16b6
replace single letter strings by characters
Vindaar Sep 10, 2025
096b5bc
extend `maybeAddType` to find types behind Ptr/UA/Array
Vindaar Sep 10, 2025
1aaf1b8
move all `builtins` into separate files, one for each backend
Vindaar Sep 10, 2025
71c379f
update doc comment of `cuda` macro
Vindaar Sep 10, 2025
0532143
improve explanation of `typeOfIndex` in CUDA's `getType` helper
Vindaar Sep 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions constantine/math_compiler/experimental/backends/backends.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,11 @@ when defined(cuda):
else:
const Backend* = bkWGSL

proc gpuTypeToString*(t: GpuTypeKind): string =
case Backend
of bkCuda: cuda.gpuTypeToString(t)
of bkWGSL: wgsl.gpuTypeToString(t)

proc gpuTypeToString*(t: GpuType, ident = newGpuIdent(), allowArrayToPtr = false,
allowEmptyIdent = false,
): string =
case Backend
of bkCuda: cuda.gpuTypeToString(t, ident.ident(), allowArrayToPtr, allowEmptyIdent)
of bkWGSL: wgsl.gpuTypeToString(t, ident, allowArrayToPtr, allowEmptyIdent)

proc genFunctionType*(typ: GpuType, fn: string, fnArgs: string): string =
case Backend
of bkCuda: cuda.genFunctionType(typ, fn, fnArgs)
of bkWGSL: wgsl.genFunctionType(typ, fn, fnArgs)

proc codegen*(ctx: var GpuContext, ast: GpuAst, kernel: string = ""): string =
case Backend
of bkCuda:
result = ctx.genCuda(ast)
cuda.preprocess(ctx, ast, kernel)
result = cuda.codegen(ctx)
of bkWGSL:
ctx.storagePass(ast, kernel)
wgsl.preprocess(ctx, ast, kernel)
result = wgsl.codegen(ctx)
31 changes: 29 additions & 2 deletions constantine/math_compiler/experimental/backends/common_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,36 @@
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.

import std / tables
import ../gpu_types
# import ./backends

proc address*(a: string): string = "&" & a

proc size*(a: string): string = "sizeof(" & a & ")"

proc isGlobal*(fn: GpuAst): bool =
doAssert fn.kind == gpuProc, "Not a function, but: " & $fn.kind
result = attGlobal in fn.pAttributes

proc farmTopLevel*(ctx: var GpuContext, ast: GpuAst, kernel: string, varBlock: var GpuAst) =
## Farms the top level of the code for functions, variable and type definition.
## All functions are added to the `allFnTab`, while only global ones (or even only
## `kernel` if any) is added to the `fnTab` as the starting point for the remaining
## logic.
## Variables are collected in `varBlock`.
case ast.kind
of gpuProc:
ctx.allFnTab[ast.pName] = ast
if kernel.len > 0 and ast.pName.ident() == kernel and ast.isGlobal():
ctx.fnTab[ast.pName] = ast.clone() # store global function extra
elif kernel.len == 0 and ast.isGlobal():
ctx.fnTab[ast.pName] = ast.clone() # store global function extra
of gpuBlock:
# could be a type definition or global variable
for ch in ast:
ctx.farmTopLevel(ch, kernel, varBlock)
of gpuVar, gpuConstexpr:
varBlock.statements.add ast
of gpuTypeDef, gpuAlias:
raiseAssert "Unexpected type def / alias def found. These should be in `ctx.types` now: " & $ast
else:
discard
Loading
Loading