source: src/Tuples/TupleExpansionNew.cpp @ bb9924c

enumforall-pointer-decaynew-ast-unique-exprpthread-emulation
Last change on this file since bb9924c was bb9924c, checked in by Fangren Yu <f37yu@…>, 9 months ago

fix unique expr pass

  • Property mode set to 100644
File size: 2.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// TupleExpansionNew.cpp --
8//
9// Author           : Henry Xue
10// Created On       : Wed Aug 18 12:54:02 2021
11// Last Modified By : Henry Xue
12// Last Modified On : Wed Aug 18 12:54:02 2021
13// Update Count     : 1
14//
15
16// Currently not working due to unresolved issues with UniqueExpr
17
18#include "Tuples.h"
19
20namespace Tuples {
21namespace {
22        struct UniqueExprExpander final : public ast::WithDeclsToAdd<> {
23                const ast::Expr * postvisit( const ast::UniqueExpr * unqExpr );
24                std::map< int, ast::ptr<ast::Expr> > decls; // not vector, because order added may not be increasing order
25        };
26} // namespace
27
28void expandUniqueExpr( ast::TranslationUnit & translationUnit ) {
29        ast::Pass< UniqueExprExpander >::run( translationUnit );
30}
31
32namespace {
33        const ast::Expr * UniqueExprExpander::postvisit( const ast::UniqueExpr * unqExpr ) {
34                const CodeLocation loc = unqExpr->location;
35                const int id = unqExpr->id;
36
37                // on first time visiting a unique expr with a particular ID, generate the expression that replaces all UniqueExprs with that ID,
38                // and lookup on subsequent hits. This ensures that all unique exprs with the same ID reference the same variable.
39                if ( ! decls.count( id ) ) {
40                        ast::ptr< ast::Expr > assignUnq;
41                        const ast::VariableExpr * var = unqExpr->var;
42                        if ( unqExpr->object ) {
43                                // an object was generated to represent this unique expression -- it should be added to the list of declarations now
44                                declsToAddBefore.push_back( unqExpr->object.as< ast::Decl >() );
45                                // deep copy required due to unresolved issues with UniqueExpr
46                                assignUnq = ast::UntypedExpr::createAssign( loc, var, unqExpr->expr );
47                        } else {
48                                const auto commaExpr = unqExpr->expr.strict_as< ast::CommaExpr >();
49                                assignUnq = commaExpr->arg1;
50                        }
51                        auto finished = new ast::ObjectDecl( loc, toString( "_unq", id, "_finished_" ), new ast::BasicType( ast::BasicType::Kind::Bool ),
52                                new ast::SingleInit( loc, ast::ConstantExpr::from_int( loc, 0 ) ), {}, ast::Linkage::Cforall );
53                        declsToAddBefore.push_back( finished );
54                        // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N))
55                        // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code.
56                        auto assignFinished = ast::UntypedExpr::createAssign( loc, new ast::VariableExpr( loc, finished ),
57                                ast::ConstantExpr::from_int( loc, 1 ) );
58                        auto condExpr = new ast::ConditionalExpr( loc, new ast::VariableExpr( loc, finished ), var,
59                                new ast::CommaExpr( loc, new ast::CommaExpr( loc, assignUnq, assignFinished ), var ) );
60                        condExpr->result = var->result;
61                        condExpr->env = unqExpr->env;
62                        decls[id] = condExpr;
63                }
64                //delete unqExpr;
65                return ast::deepCopy(decls[id].get());
66        }
67} // namespace
68} // namespace Tuples
Note: See TracBrowser for help on using the repository browser.