Skip to content

Commit ec02af6

Browse files
committed
[Heavy] Add scheme lib to emit c++ from nbdl dialect (import (nbdl comp))
1 parent 70093bc commit ec02af6

File tree

11 files changed

+238
-104
lines changed

11 files changed

+238
-104
lines changed

heavy/include/heavy/Nbdl.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===- Nbdl.h - Nbdl binding functions for HeavyScheme ----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares values and functions for the (nbdl comp) scheme library
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_HEAVY_NBDL_H
14+
#define LLVM_HEAVY_NBDL_H
15+
16+
#define HEAVY_NBDL_LIB _HEAVYL4SnbdlL4Scomp
17+
#define HEAVY_NBDL_LIB_(NAME) _HEAVYL4SnbdlL4Scomp ## NAME
18+
// (import (nbdl comp))
19+
#define HEAVY_NBDL_LIB_STR "_HEAVYL4SnbdlL4Scomp"
20+
#define HEAVY_NBDL_LOAD_MODULE HEAVY_NBDL_LIB_(_load_module)
21+
#define HEAVY_NBDL_INIT HEAVY_NBDL_LIB_(_init)
22+
23+
namespace heavy {
24+
class Context;
25+
}
26+
27+
extern "C" {
28+
// initialize the module for run-time independent of the compiler
29+
void HEAVY_NBDL_LOAD_MODULE(heavy::Context& Context);
30+
void HEAVY_NBDL_INIT(heavy::Context& Context);
31+
}
32+
33+
#endif // LLVM_HEAVY_NBDL_H

heavy/include/nbdl_gen/Dialect.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@
1313
#ifndef NBDL_GEN_DIALECT_H
1414
#define NBDL_GEN_DIALECT_H
1515

16-
#include "mlir/Dialect/Func/IR/FuncOps.h"
17-
#include "mlir/IR/BuiltinOps.h"
18-
#include "mlir/IR/Dialect.h"
19-
#include "mlir/IR/OpDefinition.h"
20-
#include "mlir/Interfaces/ControlFlowInterfaces.h"
21-
#include "mlir/Interfaces/SideEffectInterfaces.h"
16+
#include <mlir/Dialect/Func/IR/FuncOps.h>
17+
#include <mlir/IR/BuiltinOps.h>
18+
#include <mlir/IR/Dialect.h>
19+
#include <mlir/IR/OpDefinition.h>
20+
#include <mlir/Interfaces/ControlFlowInterfaces.h>
21+
#include <mlir/Interfaces/SideEffectInterfaces.h>
22+
23+
namespace nbdl_gen {
24+
using mlir::func::CallOp;
25+
using mlir::func::FuncOp;
26+
using mlir::func::ReturnOp;
27+
using mlir::StringAttr;
28+
}
2229

2330
// Include the generated header files
2431

heavy/include/nbdl_gen/Nbdl.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include "mlir/IR/SymbolInterfaces.td"
1414

1515
def Nbdl_Dialect : Dialect {
1616
let name = "nbdl";
17+
let cppNamespace = "nbdl_gen";
1718
let useDefaultTypePrinterParser = 1;
1819
}
1920

heavy/lib/Builtins.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "heavy/Builtins.h"
1414
#include "heavy/Context.h"
15+
#include "heavy/Dialect.h"
1516
#include "heavy/OpGen.h"
1617
#include "heavy/Value.h"
1718
#include "llvm/ADT/StringExtras.h"
@@ -812,6 +813,7 @@ void is_source_value(Context& C, ValueRefs Args) {
812813

813814
// initialize the module for run-time independent of the compiler
814815
void HEAVY_BASE_INIT(heavy::Context& Context) {
816+
Context.DialectRegistry->insert<heavy::Dialect>();
815817
// syntax
816818
HEAVY_BASE_VAR(define) = heavy::base::define;
817819
HEAVY_BASE_VAR(define_syntax) = heavy::base::define_syntax;

heavy/lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ add_heavy_library(heavy
1515
Quasiquote.cpp
1616
SourceFileStorage.cpp
1717
SourceManager.cpp
18+
Nbdl.cpp
1819
NbdlDialect.cpp
20+
NbdlWriter.cpp
1921
# TODO Do not use Clangs CharInfo.
2022
${CLANG_LIB_DIR}/Basic/CharInfo.cpp
2123

heavy/lib/Context.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "heavy/Lexer.h"
1717
#include "heavy/Mangle.h"
1818
#include "heavy/Mlir.h"
19+
#include "heavy/Nbdl.h"
1920
#include "heavy/OpGen.h"
2021
#include "heavy/Parser.h"
2122
#include "heavy/Source.h"
@@ -85,6 +86,7 @@ Context::Context()
8586
_HEAVY_load_module);
8687
RegisterModule(HEAVY_BASE_LIB_STR, HEAVY_BASE_LOAD_MODULE);
8788
RegisterModule(HEAVY_MLIR_LIB_STR, HEAVY_MLIR_LOAD_MODULE);
89+
RegisterModule(HEAVY_NBDL_LIB_STR, HEAVY_NBDL_LOAD_MODULE);
8890
HEAVY_BASE_INIT(*this);
8991
}
9092

heavy/lib/Mlir.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include <nbdl_gen/Dialect.h>
1413
#include <heavy/Context.h>
1514
#include <heavy/Dialect.h>
1615
#include <heavy/Mlir.h>
@@ -775,11 +774,6 @@ void verify(Context& C, heavy::ValueRefs Args) {
775774
extern "C" {
776775
// initialize the module for run-time independent of the compiler
777776
void HEAVY_MLIR_INIT(heavy::Context& C) {
778-
// TODO Register dialects in their corresponding
779-
// scheme modules instead of here.
780-
C.DialectRegistry->insert<heavy::Dialect,
781-
nbdl::NbdlDialect>();
782-
783777
mlir::MLIRContext* MC = C.MLIRContext.get();
784778
heavy::Value MC_Val = CreateTagged(C, kind::mlir_context, MC);
785779
heavy::Value BuilderVal = CreateTagged(C, kind::mlir_builder,

heavy/lib/Nbdl.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===--- Nbdl.cpp - Nbdl binding syntax for HeavyScheme ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines syntax (nbdl impl) bindings for HeavyScheme.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <heavy/Context.h>
14+
#include <heavy/Nbdl.h>
15+
#include <heavy/Value.h>
16+
#include <nbdl_gen/Dialect.h>
17+
#include <llvm/Support/Casting.h>
18+
#include <memory>
19+
#include <tuple>
20+
21+
namespace nbdl_gen {
22+
std::tuple<std::string, mlir::Location, mlir::Operation*>
23+
translate_cpp(llvm::raw_ostream& OS, mlir::Operation* Op);
24+
}
25+
26+
namespace heavy::nbdl_bind_var {
27+
heavy::ExternFunction write_cpp;
28+
}
29+
30+
namespace heavy::nbdl_bind {
31+
// Translate a nbdl dialect operation to C++.
32+
void write_cpp(Context& C, ValueRefs Args) {
33+
if (Args.size() != 2)
34+
return C.RaiseError("invalid arity");
35+
auto* Op = dyn_cast<mlir::Operation>(Args[0]);
36+
// Do not captured the emphemeral Tagged object.
37+
auto* Tagged = dyn_cast<heavy::Tagged>(Args[1]);
38+
heavy::Symbol* KindSym = C.CreateSymbol("::llvm::raw_ostream");
39+
if (!Op)
40+
return C.RaiseError("expecting mlir.operation");
41+
if (!Tagged || !Tagged->isa(KindSym))
42+
return C.RaiseError("expecting ::llvm::raw_ostream");
43+
44+
auto& OS = Tagged->cast<llvm::raw_ostream>();
45+
auto&& [ErrMsg, ErrLoc, Irritant] = nbdl_gen::translate_cpp(OS, Op);
46+
if (!ErrMsg.empty()) {
47+
auto Loc = heavy::SourceLocation(mlir::OpaqueLoc
48+
::getUnderlyingLocationOrNull<heavy::SourceLocationEncoding*>(
49+
mlir::dyn_cast<mlir::OpaqueLoc>(ErrLoc)));
50+
heavy::Error* Err = C.CreateError(Loc, ErrMsg,
51+
Irritant ? heavy::Value(Irritant) : Undefined());
52+
return C.Raise(Err);
53+
}
54+
C.Cont();
55+
}
56+
}
57+
58+
extern "C" {
59+
// initialize the module for run-time independent of the compiler
60+
void HEAVY_NBDL_INIT(heavy::Context& C) {
61+
C.DialectRegistry->insert<nbdl_gen::NbdlDialect>();
62+
63+
heavy::nbdl_bind_var::write_cpp = heavy::nbdl_bind::write_cpp;
64+
}
65+
66+
void HEAVY_NBDL_LOAD_MODULE(heavy::Context& C) {
67+
HEAVY_NBDL_INIT(C);
68+
heavy::initModuleNames(C, HEAVY_NBDL_LIB_STR, {
69+
{"write-cpp", heavy::nbdl_bind_var::write_cpp},
70+
});
71+
}
72+
}

heavy/lib/NbdlDialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define GET_OP_CLASSES
2727
#include "nbdl_gen/NbdlOps.cpp.inc"
2828

29-
void nbdl::NbdlDialect::initialize() {
29+
void nbdl_gen::NbdlDialect::initialize() {
3030
addTypes<
3131
#define GET_TYPEDEF_LIST
3232
#include "nbdl_gen/NbdlTypes.cpp.inc"

0 commit comments

Comments
 (0)