Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISPC support #92

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.cabal-sandbox
cabal.sandbox.config
/dist
.stack-work
10 changes: 10 additions & 0 deletions Language/C/Parser/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ c :-
"$recv:" / { allowAnti } { lexAnti Tanti_objc_recv }
"$kwarg:" / { allowAnti } { lexAnti Tanti_objc_arg }
"$kwargs:" / { allowAnti } { lexAnti Tanti_objc_args }

--
-- ISPC
--
"$foreachiters:" / { allowAnti } { lexAnti Tanti_foreach_iters }
}

<0> {
Expand Down Expand Up @@ -243,6 +248,11 @@ c :-

">>>" / { ifExtension cudaExts }
{ token TCUDA3gt }

--
-- ISPC
--
"in" { token TISPCin }
}

{
Expand Down
111 changes: 109 additions & 2 deletions Language/C/Parser/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,30 @@ import qualified Language.C.Syntax as C
'kernel' { L _ T.TCLkernel }
'__kernel' { L _ T.TCLkernel }

-- Three shift-reduce conflicts:
--
-- ISPC
--
'uniform' { L _ T.TISPCuniform }
'varying' { L _ T.TISPCvarying }
'foreach' { L _ T.TISPCforeach }
'export' { L _ T.TISPCexport }
'foreach_active' { L _ T.TISPCactive }
'foreach_tiled' { L _ T.TISPCtiled }
'unmasked' { L _ T.TISPCunmasked }
'foreach_unique' { L _ T.TISPCunique }
'in' { L _ T.TISPCin }
'cif' { L _ T.TISPCcif }
'cwhile' { L _ T.TISPCcwhile }
'cdo' { L _ T.TISPCcdo }
'cfor' { L _ T.TISPCcfor }
ANTI_FOREACH_ITERS { L _ (T.Tanti_foreach_iters _) }

-- Four shift-reduce conflicts:
-- (1) Documented conflict in 'objc_protocol_declaration'
-- (2) Objective-C exception syntax (would need lookahead of 2 to disambiguate properly)
-- (3) The standard dangling else conflict
%expect 3
-- (4) The dangling else conflict, but for the 'cif' construct in ISPC.
%expect 4

%monad { P } { >>= } { return }
%lexer { lexer } { L _ T.Teof }
Expand Down Expand Up @@ -343,6 +362,11 @@ import qualified Language.C.Syntax as C

%right NAMED OBJCNAMED

--
-- ISPC
--
%name parseISPCForEachIters ispc_foreach_iter_list

%%

{------------------------------------------------------------------------------
Expand Down Expand Up @@ -1306,6 +1330,10 @@ storage_class_specifier :
| '__strong' { TSObjC__strong (srclocOf $1) }
| '__unsafe_unretained' { TSObjC__unsafe_unretained (srclocOf $1) }

-- ISPC
| 'export' { TSISPCexport (srclocOf $1) }
| 'unmasked' { TSISPCunmasked (srclocOf $1) }

type_specifier :: { TySpec }
type_specifier :
'void' { TSvoid (srclocOf $1) }
Expand Down Expand Up @@ -1526,6 +1554,9 @@ type_qualifier :
| 'kernel' { TSCLkernel (srclocOf $1) }
| '__kernel' { TSCLkernel (srclocOf $1) }

| 'uniform' { TSISPCuniform (srclocOf $1) }
| 'varying' { TSISPCvarying (srclocOf $1) }

-- Consider the following C program:
--
-- typedef struct foo {
Expand Down Expand Up @@ -2151,6 +2182,12 @@ selection_statement :
{ Switch $3 $5 ($1 `srcspan` $5) }
| 'switch' '(' expression error
{% unclosed ($2 <--> $3) "(" }

-- ISPC
| 'cif' '(' expression ')' statement
{ CIf $3 $5 Nothing ($1 `srcspan` $5) }
| 'cif' '(' expression ')' statement 'else' statement
{ CIf $3 $5 (Just $7) ($1 `srcspan` $7) }

iteration_statement :: { Stm }
iteration_statement :
Expand All @@ -2177,6 +2214,28 @@ iteration_statement :
| 'for' '(' maybe_expression_nlt semi maybe_expression semi expression error
{% unclosed ($2 <--> $7) "(" }

-- ISPC
| 'foreach' '(' ispc_foreach_iter_list ')' statement
{ ForEach ($3) ($5) ($1 `srcspan` $5) }
| 'foreach_active' '(' identifier ')' statement
{ ForEachActive ($3) ($5) ($1 `srcspan` $5) }
| 'foreach_tiled' '(' ispc_foreach_iter_list ')' statement
{ ForEachTiled ($3) ($5) ($1 `srcspan` $5) }
| 'foreach_unique' '(' identifier 'in' expression ')' statement
{ ForEachUnique ($3) ($5) ($7) ($1 `srcspan` $7) }
| 'cwhile' '(' expression ')' statement
{ CWhile $3 $5 ($1 `srcspan` $5) }
| 'cdo' statement 'while' '(' expression ')' ';'
{ CDo $2 $5 ($1 `srcspan` $7) }
| 'cfor' '(' declaration maybe_expression semi ')' statement
{ CFor (Left $3) $4 Nothing $7 ($1 `srcspan` $7) }
| 'cfor' '(' maybe_expression_nlt semi maybe_expression semi ')' statement
{ CFor (Right $3) $5 Nothing $8 ($1 `srcspan` $8) }
| 'cfor' '(' declaration maybe_expression semi expression ')' statement
{ CFor (Left $3) $4 (Just $6) $8 ($1 `srcspan` $8) }
| 'cfor' '(' maybe_expression_nlt semi maybe_expression semi expression ')' statement
{ CFor (Right $3) $5 (Just $7) $9 ($1 `srcspan` $9) }

jump_statement :: { Stm }
jump_statement :
'goto' identifier ';' { Goto $2 ($1 `srcspan` $3) }
Expand All @@ -2191,6 +2250,25 @@ jump_statement :
| 'return' expression ';' { Return (Just $2) ($1 `srcspan` $3) }
| 'return' expression error {% expected ["';'"] Nothing }

ispc_foreach_iter :: { ForEachIter }
ispc_foreach_iter :
identifier '=' assignment_expression '...' assignment_expression { ForEachIter ($1) ($3) ($5) ($1 `srcspan` $5) }

ispc_foreach_iter_list :: { [ForEachIter] }
ispc_foreach_iter_list :
ispc_foreach_iter_rlist { rev $1 }

ispc_foreach_iter_rlist :: { RevList ForEachIter }
ispc_foreach_iter_rlist :
ispc_foreach_iter
{ rsingleton $1 }
| ANTI_FOREACH_ITERS
{ rsingleton (AntiForEachIters (getANTI_FOREACH_ITERS $1) (srclocOf $1)) }
| ispc_foreach_iter_rlist ',' ispc_foreach_iter
{ rcons $3 $1}
| ispc_foreach_iter_rlist ',' ANTI_ARGS
{ rcons (AntiForEachIters (getANTI_FOREACH_ITERS $3) (srclocOf $3)) $1 }

{------------------------------------------------------------------------------
-
- External definitions
Expand Down Expand Up @@ -3432,6 +3510,11 @@ getANTI_OBJC_RECV (L _ (T.Tanti_objc_recv v)) = v
getANTI_OBJC_ARG (L _ (T.Tanti_objc_arg v)) = v
getANTI_OBJC_ARGS (L _ (T.Tanti_objc_args v)) = v

--
-- ISPC
--
getANTI_FOREACH_ITERS (L _ (T.Tanti_foreach_iters v)) = v

lexer :: (L T.Token -> P a) -> P a
lexer cont = do
t <- lexToken
Expand Down Expand Up @@ -3508,6 +3591,12 @@ data TySpec = TSauto !SrcLoc
| TSCLreadonly !SrcLoc
| TSCLwriteonly !SrcLoc
| TSCLkernel !SrcLoc

-- ISPC
| TSISPCuniform !SrcLoc
| TSISPCvarying !SrcLoc
| TSISPCexport !SrcLoc
| TSISPCunmasked !SrcLoc
deriving (Eq, Ord, Show)

instance Located TySpec where
Expand Down Expand Up @@ -3574,6 +3663,11 @@ instance Located TySpec where
locOf (TSCLwriteonly loc) = locOf loc
locOf (TSCLkernel loc) = locOf loc

locOf (TSISPCuniform loc) = locOf loc
locOf (TSISPCvarying loc) = locOf loc
locOf (TSISPCexport loc) = locOf loc
locOf (TSISPCunmasked loc) = locOf loc

instance Pretty TySpec where
ppr (TSauto _) = text "auto"
ppr (TSregister _) = text "register"
Expand Down Expand Up @@ -3642,6 +3736,11 @@ instance Pretty TySpec where
ppr (TSCLwriteonly _) = text "write_only"
ppr (TSCLkernel _) = text "__kernel"

ppr (TSISPCuniform _) = text "uniform"
ppr (TSISPCvarying _) = text "varying"
ppr (TSISPCexport _) = text "export"
ppr (TSISPCunmasked _) = text "unmasked"

isStorage :: TySpec -> Bool
isStorage (TSauto _) = True
isStorage (TSregister _) = True
Expand All @@ -3652,6 +3751,8 @@ isStorage (TS__block _) = True
isStorage (TSObjC__weak _) = True
isStorage (TSObjC__strong _) = True
isStorage (TSObjC__unsafe_unretained _) = True
isStorage (TSISPCexport _) = True
isStorage (TSISPCunmasked _) = True
isStorage _ = False

mkStorage :: [TySpec] -> [Storage]
Expand All @@ -3667,6 +3768,8 @@ mkStorage specs = map mk (filter isStorage specs)
mk (TSObjC__weak loc) = TObjC__weak loc
mk (TSObjC__strong loc) = TObjC__strong loc
mk (TSObjC__unsafe_unretained loc) = TObjC__unsafe_unretained loc
mk (TSISPCexport loc) = TISPCexport loc
mk (TSISPCunmasked loc) = TISPCunmasked loc
mk _ = error "internal error in mkStorage"

isTypeQual :: TySpec -> Bool
Expand All @@ -3692,6 +3795,8 @@ isTypeQual (TSCLconstant _) = True
isTypeQual (TSCLreadonly _) = True
isTypeQual (TSCLwriteonly _) = True
isTypeQual (TSCLkernel _) = True
isTypeQual (TSISPCuniform _) = True
isTypeQual (TSISPCvarying _) = True
isTypeQual _ = False

mkTypeQuals :: [TySpec] -> [TypeQual]
Expand Down Expand Up @@ -3720,6 +3825,8 @@ mkTypeQuals specs = map mk (filter isTypeQual specs)
mk (TSCLreadonly loc) = TCLreadonly loc
mk (TSCLwriteonly loc) = TCLwriteonly loc
mk (TSCLkernel loc) = TCLkernel loc
mk (TSISPCuniform loc) = TISPCuniform loc
mk (TSISPCvarying loc) = TISPCvarying loc
mk _ = error "internal error in mkTypeQual"

isSign :: TySpec -> Bool
Expand Down
59 changes: 57 additions & 2 deletions Language/C/Parser/Tokens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ data Token = Teof
| TCLwriteonly
| TCLkernel

-- ISPC
| TISPCuniform
| TISPCvarying
| TISPCforeach
| TISPCexport
| TISPCactive
| TISPCtiled
| TISPCunmasked
| TISPCunique
| TISPCin
| TISPCcwhile
| TISPCcif
| TISPCcfor
| TISPCcdo
| Tanti_foreach_iters String

-- Clang (currently active is Objective-C is active)
| T__block

Expand Down Expand Up @@ -340,6 +356,11 @@ instance Show Token where
show (Tanti_objc_arg s) = showAnti "kwarg" s
show (Tanti_objc_args s) = showAnti "kwargs" s

--
-- ISPC
--
show (Tanti_foreach_iters s) = showAnti "foreachiters" s

show t = fromMaybe (error "language-c-quote: internal error: unknown token")
(lookup t tokenStrings)

Expand Down Expand Up @@ -522,7 +543,24 @@ tokenStrings = [(Tlparen, "("),
(TCLconstant, "__constant"),
(TCLreadonly, "read_only"),
(TCLwriteonly, "write_only"),
(TCLkernel, "__kernel")
(TCLkernel, "__kernel"),

--
-- ISPC extensions
--
(TISPCuniform, "uniform"),
(TISPCvarying, "varying"),
(TISPCforeach, "foreach"),
(TISPCexport, "export"),
(TISPCactive, "foreach_active"),
(TISPCtiled, "foreach_tiled"),
(TISPCunmasked, "unmasked"),
(TISPCunique, "unique"),
(TISPCin , "in"),
(TISPCcif, "cif"),
(TISPCcwhile, "cwhile"),
(TISPCcdo, "cdo"),
(TISPCcfor, "cfor")
]

keywords :: [(String, Token, Maybe [Extensions])]
Expand Down Expand Up @@ -653,7 +691,24 @@ keywords = [("auto", Tauto, Nothing),
("write_only", TCLwriteonly, Just [OpenCL]),
("__write_only", TCLwriteonly, Just [OpenCL]),
("kernel", TCLkernel, Just [OpenCL]),
("__kernel", TCLkernel, Just [OpenCL])
("__kernel", TCLkernel, Just [OpenCL]),

--
-- ISPC
--
("uniform", TISPCuniform, Just [ISPC]),
("varying", TISPCvarying, Just [ISPC]),
("foreach", TISPCforeach, Just [ISPC]),
("export", TISPCexport, Just [ISPC]),
("foreach_tiled",TISPCtiled, Just [ISPC]),
("foreach_active",TISPCactive, Just [ISPC]),
("unmasked", TISPCunmasked, Just [ISPC]),
("foreach_unique",TISPCunique, Just [ISPC]),
("in" ,TISPCin , Just [ISPC]),
("cif", TISPCcif, Just [ISPC]),
("cwhile", TISPCcwhile, Just [ISPC]),
("cdo", TISPCcdo, Just [ISPC]),
("cfor", TISPCcfor, Just [ISPC])
]

type ExtensionsInt = Word32
Expand Down
Loading