Skip to content

Commit

Permalink
Introduce isType and workaround nim-lang/Nim#6785 and nim-lang/RFCs#44
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Jul 7, 2019
1 parent bdae4e2 commit c921961
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
6 changes: 4 additions & 2 deletions laser/dynamic_stack_arrays.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

const LASER_MAXRANK*{.intdefine.} = 6
# On x86-64, a cache line can contain 8 int64. Hence for best performance
# MAXRANK should be at most 8 on x86_64 machines
# Unless, you want to index using int32 which limits dimensions
# MAXRANK should be at most 7 on x86_64 machines.
# The extra int is used to store the real rank.
#
# Alternatively, you may index using int32 which limits dimensions
# tensor dimensions to 2 billions elements.

type DynamicStackArray*[T] = object
Expand Down
6 changes: 2 additions & 4 deletions laser/lux/ast/ast_compiler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ proc initParams(
let ident = iddefs[j]
result.ids.add ident
# Ident base type (without seq)
if iddefs[^2].kind == nnkBracketExpr and
iddefs[^2][0].eqIdent"seq":
if iddefs[^2].isType"seq":
result.ids_baseType.add iddefs[^2][1]
else:
result.ids_baseType.add iddefs[^2]
Expand Down Expand Up @@ -81,8 +80,7 @@ proc initParams(
let ident = iddefs[j]
result.ids.add ident
# Ident base type (without seq)
if iddefs[^2].kind == nnkBracketExpr and
iddefs[^2][0].eqIdent"seq":
if iddefs[^2].isType"seq":
result.ids_baseType.add iddefs[^2][1]
else:
result.ids_baseType.add iddefs[^2]
Expand Down
19 changes: 15 additions & 4 deletions laser/lux/ast/macro_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ proc ct*(ident: NimNode): NimNode =
)
)

{.experimental: "dynamicBindSym".}
proc isType*(x: NimNode, t: string): bool =
## Compile-time type checking

# We cannot instantiate a fully typed container
# https://github.com/nim-lang/Nim/issues/6785
# and https://github.com/nim-lang/RFCs/issues/44

if x.kind == nnkBracketExpr:
return sameType(bindSym(x[0]), bindSym(t))
else:
return sameType(bindSym(x), bindSym(t))

proc liftTypes*(
ast: NimNode,
containerIdent: string,
Expand All @@ -55,11 +68,9 @@ proc liftTypes*(
of nnkEmpty: return node
of nnkLiterals: return node
of nnkIdentDefs:
let i = node.len - 2 # Type position
if node[i].kind == nnkBracketExpr and
node[i][0].eqIdent(containerIdent):
if node[^2].isType(containerIdent):
result = node.copyNimTree()
result[i] = remapping(node[i][1])
result[^2] = remapping(node[^2][1])
return
else:
return node
Expand Down
2 changes: 1 addition & 1 deletion laser/lux/platforms/platform_x86.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,4 @@ proc SimdMap*(arch: SimdArch, T: NimNode, p: SimdPrimitives): NimNode =
elif T.eqIdent"float64":
result = MapX86Float64[arch][p]
else:
error "Unsupported type: \"" & $T & '\"'
error "Unsupported type: \"" & T.repr & '\"'

0 comments on commit c921961

Please sign in to comment.