Commit 94183cc4 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Condition the callable export generation



Using #pragma { export; } annotation

Fixed an issue in meta #pragma parsing (boolean condition)

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent b45c0c6b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
OPENQASM 3;
include "stdgates.inc";

#pragma { export; }
def qasm_x qubit:qb {
  print("Hello from QASM3!");
  x qb;
+2 −0
Original line number Diff line number Diff line
@@ -3,10 +3,12 @@
OPENQASM 3;
include "stdgates.inc";

#pragma { export; }
def qasm_x qubit:qb {
  x qb;
}

#pragma { export; }
def qasm_h qubit:qb {
  h qb;
}
 No newline at end of file
+16 −1
Original line number Diff line number Diff line
@@ -168,6 +168,18 @@ class qasm3_visitor : public qasm3::qasm3BaseVisitor {
    return 0;
  }
  
  antlrcpp::Any visitPragma(qasm3Parser::PragmaContext *ctx) override {
    // Handle the #pragma { export; } directive
    // Mark the export bool flag so that the later sub-routine handler will pick it up.
    if (ctx->statement().size() == 1 && ctx->statement(0)->getText() == "export;") {
      // The handler needs to reset this flag after handling the sub-routine.
      assert(!export_subroutine_as_callable);
      export_subroutine_as_callable = true;
      return 0;
    } else {
      return visitChildren(ctx);
    }
  }
 protected:
  // Reference to the MLIR OpBuilder and ModuleOp
  // this MLIRGen task
@@ -188,6 +200,9 @@ class qasm3_visitor : public qasm3::qasm3BaseVisitor {
  // return statement for subroutines
  bool subroutine_return_statment_added = false;
  bool is_return_stmt = false;
  // Flag to indicate that we should add a callable export
  // for the next subroutine. 
  bool export_subroutine_as_callable = false;
  // Keep track of expected subroutine return type
  mlir::Type current_function_return_type;

+8 −2
Original line number Diff line number Diff line
@@ -168,6 +168,8 @@ void add_callable_gen(mlir::OpBuilder &builder, const std::string &func_name,
  }

  // Add a function to create the callable wrapper for this kernel
  // Naming convention: <Kernel Name>__callable()
  // Returns a Callable* wrapping the underlying kernel (via the above 4 functors) 
  auto create_callable_func_type = builder.getFunctionType({}, callable_type);
  const std::string create_callable_fn_name = func_name + "__callable";
  auto create_callable_func_proto =
@@ -392,8 +394,12 @@ antlrcpp::Any qasm3_visitor::visitSubroutineDefinition(

  m_module.push_back(interop);

  // TODO: add a compile switch to enable/disable this export:
  // There is a #pragma {export;} directive above this kernel
  // generate the export function.
  if (export_subroutine_as_callable) {
    add_callable_gen(builder, subroutine_name, m_module, function);
    export_subroutine_as_callable = false;
  }
  return 0;
}

+1 −1
Original line number Diff line number Diff line
@@ -449,7 +449,7 @@ def main(argv=None):
        # process to single file
        text = 'OPENQASM 3;\n' + \
            '\n'.join([line for line in result.split('\n')
                       if 'OPENQASM' not in line and len(line) > 0 and ('#pragma' not in line and 'no-entrypoint' not in line)])
                       if 'OPENQASM' not in line and len(line) > 0 and (not ('#pragma' in line and 'no_entrypoint' in line))])

        base_name = os.path.splitext(filename)[0].split(os.path.sep)[-1]