Skip to content

Commit fe0b0fd

Browse files
committed
wip: adding native flag to raw binops, but not using it yet
1 parent d260d73 commit fe0b0fd

File tree

6 files changed

+51
-25
lines changed

6 files changed

+51
-25
lines changed

compiler/src/IR2Raw.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,19 @@ _raisePC :: RawVar -> TM ()
246246
_raisePC raiseBy = do
247247
pc <- getPC
248248
pc' <- freshRawVarWith "_pc_"
249-
tell [ AssignRaw pc' (Bin Basics.LatticeJoin pc raiseBy)
249+
tell [ AssignRaw pc' (_default_bin Basics.LatticeJoin pc raiseBy)
250250
, SetState MonPC pc'
251251
]
252252

253+
_default_bin op r1 r2 = Bin op (UseNativeBinop False) r1 r2
254+
253255
-- | Generate instructions raising the blocking label with the label in the given variable.
254256
-- Not to be used directly, see functions below instead.
255257
_raiseBlock :: RawVar -> TM ()
256258
_raiseBlock raiseBy = do
257259
bl <- getBlock
258260
bl' <- freshRawVarWith "_bl_"
259-
tell [ AssignRaw bl' (Bin Basics.LatticeJoin bl raiseBy)
261+
tell [ AssignRaw bl' (_default_bin Basics.LatticeJoin bl raiseBy)
260262
, SetState MonBlock bl'
261263
]
262264

@@ -351,7 +353,7 @@ compLabel = \case
351353
then return r
352354
else foldM (\(r1 :: RawVar) (r2 :: RawVar) -> do
353355
r' :: RawVar <- freshRawVarWith "_lbl_"
354-
tell [ AssignRaw r' $ Bin Basics.LatticeJoin r1 r2 ]
356+
tell [ AssignRaw r' $ _default_bin Basics.LatticeJoin r1 r2 ]
355357
return r'
356358
) r rs
357359
PC -> getPC
@@ -500,7 +502,7 @@ expr2rawComp = \case
500502
-- (for operations where the result type is fixed).
501503
let basicBinOpComp =
502504
return SimpleRawComp
503-
{ cVal = RBin v1 v2 $ Bin op
505+
{ cVal = RBin v1 v2 $ Bin op (UseNativeBinop True)
504506
, cValLbl = Join PC (ValLbl v1) [ValLbl v2]
505507
, cTyLbl = PC
506508
}
@@ -558,12 +560,12 @@ expr2rawComp = \case
558560
-- Note: Even though the result depends on the types of the parameters, it is sufficient to join their
559561
-- value labels into the result's value label, due to the invariant tyLbl ⊑ valLbl.
560562
Basics.Eq -> return ComplexRawComp
561-
{ ccVal = RBin v1 v2 $ Bin op
563+
{ ccVal = RBin v1 v2 $ Bin op (UseNativeBinop False)
562564
, ccValLbl = \resValLbl -> Join PC (ValLbl v1) [ValLbl v2, resValLbl]
563565
, ccTyLbl = const PC -- The result type is always boolean
564566
}
565567
Basics.Neq -> return ComplexRawComp
566-
{ ccVal = RBin v1 v2 $ Bin op
568+
{ ccVal = RBin v1 v2 $ Bin op (UseNativeBinop False)
567569
, ccValLbl = \resValLbl -> Join PC (ValLbl v1) [ValLbl v2, resValLbl]
568570
, ccTyLbl = const PC -- The result type is always boolean
569571
}

compiler/src/Raw.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ data RTAssertion
9999
-- What would be possible is to introduce a pre-processing which translates IR expressions into
100100
-- categorized expressions, which could then slightly simplify handling at IR2Raw.
101101
data RawExpr
102-
= Bin Basics.BinOp RawVar RawVar
102+
= Bin Basics.BinOp UseNativeBinop RawVar RawVar
103103
| Un Basics.UnaryOp RawVar
104104
| ProjectLVal VarAccess LValField
105105
| ProjectState MonComponent
@@ -119,6 +119,12 @@ data RawExpr
119119
| ConstructLVal RawVar RawVar RawVar
120120
deriving (Eq, Show)
121121

122+
-- | For equality and inequality, we generally defer to the runtime. However
123+
-- when we know that the operation involves simple types we can generate
124+
-- faster code, avoiding calling the runtime functions
125+
--
126+
newtype UseNativeBinop = UseNativeBinop Bool
127+
deriving (Eq, Show)
122128

123129
data RawInst
124130
-- | Assign the result of the given simple expression (an unlabelled value) to the given raw variable.
@@ -229,7 +235,7 @@ data InstructionType
229235

230236
instructionType :: RawInst -> InstructionType
231237
instructionType i = case i of
232-
AssignRaw _ (Bin Basics.LatticeJoin _ _) -> LabelSpecificInstruction
238+
AssignRaw _ (Bin Basics.LatticeJoin _ _ _) -> LabelSpecificInstruction
233239
AssignRaw _ (ProjectState MonPC) -> LabelSpecificInstruction
234240
AssignRaw _ (ProjectState MonBlock) -> LabelSpecificInstruction
235241
AssignRaw _ (ProjectState R0_Lev) -> LabelSpecificInstruction
@@ -268,7 +274,7 @@ ppFunDef ( FunDef hfn consts insts _ )
268274

269275

270276
ppRawExpr :: RawExpr -> PP.Doc
271-
ppRawExpr (Bin binop va1 va2) =
277+
ppRawExpr (Bin binop _ va1 va2) = -- TODO: 2025-07-31; also print the fast flag
272278
ppId va1 <+> text (show binop) <+> ppId va2
273279
ppRawExpr (Un op v) =
274280
text (show op) <> PP.parens (ppId v)

compiler/src/RawDefUse.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ instance Trav a => Trav [a]
192192
instance Usable RawExpr b where
193193
use e =
194194
case e of
195-
Raw.Bin _ x y -> use [x,y]
195+
Raw.Bin _ _ x y -> use [x,y]
196196
Raw.Un _ x -> use x
197197
Raw.ProjectLVal x _ -> use x
198198
Raw.ProjectState _ -> return ()

compiler/src/RawOpt.hs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ instance Substitutable RawVar where
4444
instance Substitutable RawExpr where
4545
apply subst e =
4646
case e of
47-
Bin op x y -> Bin op (apply subst x) (apply subst y)
47+
Bin op use_native x y -> Bin op use_native (apply subst x) (apply subst y)
4848
Un op x -> Un op (apply subst x)
4949
ListCons x y -> ListCons x (apply subst y)
5050
WithRecord x fs -> WithRecord (apply subst x) fs
@@ -95,7 +95,8 @@ data PState =
9595
stateJoins :: Map (RawVar, RawVar) RawVar, -- computed joins
9696
stateSubst :: Subst,
9797
stateChange:: ChangeFlag,
98-
stateTypes :: Map RawVar RawType -- for assertions optimizations
98+
stateRawVarTypes :: Map RawVar RawType, -- for assertions optimizations
99+
stateLValTypes :: Map VarName RawType -- for assertions optimizations
99100
}
100101

101102
-- 2021-02-28; AA
@@ -136,7 +137,7 @@ instance MarkUsed a => MarkUsed [a] where
136137

137138
instance MarkUsed RawExpr where
138139
markUsed e = case e of
139-
Bin _ x y -> markUsed [x,y]
140+
Bin _ _ x y -> markUsed [x,y]
140141
Un _ x -> markUsed x
141142
ProjectLVal x _ -> markUsed x
142143
ProjectState _ -> return ()
@@ -191,7 +192,7 @@ guessType :: RawExpr -> Maybe RawType
191192
guessType = \case
192193
Const lit -> typeOfLit lit
193194

194-
Bin op _ _ -> case op of
195+
Bin op _ _ _ -> case op of
195196
Basics.Plus -> Just RawNumber
196197
Basics.Minus -> Just RawNumber
197198
Basics.Div -> Just RawNumber
@@ -256,8 +257,11 @@ guessType = \case
256257
Base _ -> Nothing
257258
ConstructLVal _ _ _ -> Nothing
258259

259-
_setType x t = modify (\pstate ->
260-
pstate { stateTypes = Map.insert x t (stateTypes pstate)})
260+
_setRawType x t = modify (\pstate ->
261+
pstate { stateRawVarTypes = Map.insert x t (stateRawVarTypes pstate)})
262+
263+
_setLValType x t = modify (\pstate ->
264+
pstate { stateLValTypes = Map.insert x t (stateLValTypes pstate)})
261265

262266
-- Partially evaluate instruction. This is called multiple times in the optimization sequence.
263267
-- First pass: partially evaluate functions (instructions).
@@ -275,7 +279,7 @@ pevalInst i = do
275279
case monLookup p pstate of -- lookup the known state of the monitor component
276280
Just r' -> _omit $ addSubst r r' -- The state can already be found in r', therefore the assignment to r can be omitted, and we have to remember to substitute r with r'.
277281
Nothing -> _keep $ monInsert p r -- remember that PC/block can be found in variable r
278-
AssignRaw r (Bin Basics.LatticeJoin x y) -> do
282+
AssignRaw r (Bin Basics.LatticeJoin (UseNativeBinop False) x y) -> do
279283
if x == y then _omit (addSubst r x) -- trivial join
280284
else do
281285
case Map.lookup (x,y) (stateJoins pstate) of
@@ -303,21 +307,34 @@ pevalInst i = do
303307
let m0 = stateLVals pstate
304308
let m1 = Map.insert (v, field) r m0
305309
put $ pstate { stateLVals = m1 }
310+
311+
-- 2025-07-31; now also examine the type information
312+
-- which is useful for booleans
313+
case (Map.lookup v (stateLValTypes pstate)) of
314+
Nothing -> return ()
315+
Just t -> _setRawType r t
316+
306317
AssignRaw r rexpr -> _keep $ do
307318
markUsed rexpr
308319
case guessType rexpr of
309320
Nothing -> return ()
310-
Just ty -> _setType r ty
321+
Just ty -> _setRawType r ty
311322

312323

313-
AssignLVal v complexExpr -> _keep $ markUsed complexExpr
324+
AssignLVal v complexExpr -> _keep $ do
325+
case complexExpr of
326+
Bin op (UseNativeBinop False) r1 r2 | op `elem` [Basics.Eq, Basics.Neq] ->
327+
_setLValType v RawBoolean
328+
_ -> return ()
329+
_keep $ markUsed complexExpr
330+
314331
SetState p r -> _keep $ do
315332
markUsed r
316333
monInsert p r
317334
RTAssertion (AssertType r rt) -> do
318-
case Map.lookup r (stateTypes pstate) of
335+
case Map.lookup r (stateRawVarTypes pstate) of
319336
Just rt' | rt' == rt -> return Nothing
320-
_ -> _keep $ _setType r rt >> markUsed r
337+
_ -> _keep $ _setRawType r rt >> markUsed r
321338
-- RTAssertion (AssertEqTypes opt_ls x y) -> do
322339
-- let _m = stateTypes pstate
323340
-- let keep = _keep $ markUsed [x,y]
@@ -331,7 +348,7 @@ pevalInst i = do
331348
-- else keep
332349
-- _ -> keep
333350
RTAssertion (AssertTypesBothStringsOrBothNumbers x y) -> do
334-
let _m = stateTypes pstate
351+
let _m = stateRawVarTypes pstate
335352
let keep = _keep $ markUsed [x,y]
336353
case (Map.lookup x _m, Map.lookup y _m) of
337354
(Just t1 , Just t2) | t1 == t2 ->
@@ -518,7 +535,8 @@ funopt (FunDef hfn consts bb ir) =
518535
stateJoins = Map.empty,
519536
stateSubst = Subst (m_subst),
520537
stateChange = False,
521-
stateTypes = constTypes
538+
stateRawVarTypes = constTypes,
539+
stateLValTypes = Map.empty
522540
}
523541
(bb', _, _) = runRWS (peval bb) () pstate
524542
new = FunDef hfn consts' bb' ir

compiler/src/Stack2JS.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ instance ToJS RawExpr where
583583
Raw.FieldValLev -> monStateToJs MonPC
584584
Raw.FieldTypLev -> monStateToJs MonPC)
585585
e@(ProjectLVal _ _) -> return $ ppRawExpr e
586-
Bin binop va1 va2 -> return $
586+
Bin binop _ va1 va2 -> return $
587587
let text' = (text . binOpToJS) binop in
588588
if isInfixBinop binop
589589
then hsep [ ppId va1, text', ppId va2 ]

tests/_unautomated/loop.trp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let
22
fun loop n =
3-
if n <= 10 then
3+
if n = 10 then
44
let val _ = printWithLabels n
55
in loop (n + 1)
66
end

0 commit comments

Comments
 (0)