Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 3f96772

Browse files
committed
initial link deferredrunner->jit
1 parent f0fd1ab commit 3f96772

File tree

6 files changed

+95
-11
lines changed

6 files changed

+95
-11
lines changed

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ def build_cmake(self, ext):
3333
cmake_args = [
3434
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + str(extdir.parent.absolute()),
3535
'-DCMAKE_BUILD_TYPE=' + config,
36-
'-DCMAKE_VERBOSE_MAKEFILE=ON'
36+
'-DCMAKE_VERBOSE_MAKEFILE=ON',
37+
'-G=Ninja',
3738
]
3839

3940
# example of build args
4041
build_args = [
4142
'--config', config,
42-
'--', '-j8'
43+
# '--', '-j8'
4344
]
4445

4546
os.chdir(str(build_temp))

src/Creator.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "ddptensor/Deferred.hpp"
55
#include "ddptensor/Factory.hpp"
66

7+
#include <imex/Dialect/PTensor/IR/PTensorOps.h>
8+
#include <mlir/IR/Builders.h>
9+
710
namespace x {
811

912
template<typename T>
@@ -141,10 +144,25 @@ struct DeferredArange : public Deferred
141144
: _start(start), _end(end), _step(step), _dtype(dtype)
142145
{}
143146

144-
void run()
147+
void run() override
145148
{
146149
set_value(std::move(TypeDispatch<x::Creator>(_dtype, _start, _end, _step)));
147150
};
151+
152+
::mlir::Value generate_mlir(::mlir::OpBuilder & builder, ::mlir::Location loc, jit::IdValueMap & ivm) override
153+
{
154+
// create start, stop and step
155+
auto start = jit::createI64(loc, builder, _start);
156+
auto end = jit::createI64(loc, builder, _end);
157+
auto step = jit::createI64(loc, builder, _step);
158+
// create arange
159+
auto dtype = builder.getI64Type();
160+
llvm::SmallVector<int64_t> shape(1, -1); //::mlir::ShapedType::kDynamicSize);
161+
auto artype = ::imex::ptensor::PTensorType::get(builder.getContext(), ::mlir::RankedTensorType::get(shape, dtype), true);
162+
auto ar = builder.create<::imex::ptensor::ARangeOp>(loc, artype, start, end, step, true);
163+
ivm[_guid] = ar;
164+
return ar;
165+
}
148166

149167
FactoryId factory() const
150168
{

src/Deferred.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// SPDX-License-Identifier: BSD-3-Clause
22

3-
#include <oneapi/tbb/concurrent_queue.h>
43
#include "include/ddptensor/Deferred.hpp"
54
#include "include/ddptensor/Transceiver.hpp"
65
#include "include/ddptensor/Mediator.hpp"
76
#include "include/ddptensor/Registry.hpp"
87

8+
#include <oneapi/tbb/concurrent_queue.h>
9+
#include <mlir/Dialect/Func/IR/FuncOps.h>
10+
#include <imex/Dialect/PTensor/IR/PTensorOps.h>
11+
#include <mlir/Dialect/LLVMIR/LLVMDialect.h>
12+
913
static tbb::concurrent_bounded_queue<Runable::ptr_type> _deferred;
1014

1115
void push_runable(Runable::ptr_type && r)
@@ -55,13 +59,46 @@ void Runable::fini()
5559

5660
void process_promises()
5761
{
62+
jit::JIT jit;
63+
::mlir::OpBuilder builder(&jit._context);
64+
auto loc = builder.getUnknownLoc();
65+
jit::IdValueMap ivp;
66+
::mlir::Value ret_value;
67+
68+
// Create a MLIR module
69+
auto module = builder.create<::mlir::ModuleOp>(loc);
70+
// Create a func
71+
auto dtype = builder.getI64Type();
72+
llvm::SmallVector<int64_t> shape(1, -1); //::mlir::ShapedType::kDynamicSize);
73+
auto rrtype = ::imex::ptensor::PTensorType::get(builder.getContext(), ::mlir::RankedTensorType::get(shape, dtype), true); // llvm::SmallVector<int64_t>()
74+
auto funcType = builder.getFunctionType({}, rrtype);
75+
std::string fname("ddpt_jit");
76+
auto function = builder.create<::mlir::func::FuncOp>(loc, fname, funcType);
77+
// request generation of c-wrapper function
78+
function->setAttr(::mlir::LLVM::LLVMDialect::getEmitCWrapperAttrName(), ::mlir::UnitAttr::get(&jit._context));
79+
// create function entry block
80+
auto &entryBlock = *function.addEntryBlock();
81+
// Set the insertion point in the builder to the beginning of the function body
82+
builder.setInsertionPointToStart(&entryBlock);
83+
5884
while(true) {
5985
Runable::ptr_type d;
6086
_deferred.pop(d);
61-
if(d) d->run();
62-
else break;
63-
d.reset();
87+
if(d) {
88+
d->run();
89+
ret_value = d->generate_mlir(builder, loc, ivp);
90+
d.reset();
91+
} else {
92+
break;
93+
}
6494
}
95+
96+
(void)builder.create<::mlir::func::ReturnOp>(loc, ret_value);
97+
// add the function to the module
98+
module.push_back(function);
99+
module.dump();
100+
// finally compile and run the module
101+
if(jit.run(module, fname)) throw std::runtime_error("failed running jit");
65102
}
66103

67104
void sync()

src/include/ddptensor/Deferred.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "tensor_i.hpp"
66
#include "Registry.hpp"
7+
#include "jit/mlir.hpp"
78

89
extern void process_promises();
910
extern void sync();
@@ -12,7 +13,10 @@ struct Runable
1213
{
1314
using ptr_type = std::unique_ptr<Runable>;
1415
virtual ~Runable() {};
16+
/// actually execute, a deferred will set value of future
1517
virtual void run() = 0;
18+
/// generate MLIR code for jit
19+
virtual ::mlir::Value generate_mlir(::mlir::OpBuilder & builder, ::mlir::Location, jit::IdValueMap & ivm) = 0;
1620
virtual FactoryId factory() const = 0;
1721
virtual void defer(ptr_type &&);
1822
static void fini();
@@ -29,13 +33,20 @@ struct DeferredT : public P, public Runable
2933

3034
DeferredT() = default;
3135
DeferredT(const DeferredT<P, F> &) = delete;
36+
37+
// FIXME: from Runable but should be in most derived classes
38+
::mlir::Value generate_mlir(::mlir::OpBuilder &, ::mlir::Location, jit::IdValueMap &) override
39+
{
40+
return {};
41+
};
3242
};
3343

3444
struct Deferred : public DeferredT<tensor_i::promise_type, tensor_i::future_type>
3545
{
3646
using ptr_type = std::unique_ptr<Deferred>;
3747
id_type _guid = Registry::NOGUID;
3848
future_type get_future();
49+
// from Runable
3950
void defer(Runable::ptr_type &&);
4051
};
4152

@@ -98,6 +109,13 @@ struct DeferredLambda : public Runable
98109
_l();
99110
}
100111

112+
// FIXME: from Runable but should be in most derived classes
113+
::mlir::Value generate_mlir(::mlir::OpBuilder &, ::mlir::Location, jit::IdValueMap &) override
114+
{
115+
throw(std::runtime_error("No MLIR support for DeferredLambda."));
116+
return {};
117+
};
118+
101119
FactoryId factory() const
102120
{
103121
throw(std::runtime_error("No Factory for DeferredLambda."));

src/include/ddptensor/jit/mlir.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22

33
#pragma once
44

5-
#include "mlir/IR/BuiltinOps.h"
6-
#include "mlir/IR/MLIRContext.h"
7-
#include "mlir/Pass/PassManager.h"
5+
#include "ddptensor/UtilsAndTypes.hpp"
6+
7+
#include <mlir/IR/BuiltinOps.h>
8+
#include <mlir/IR/MLIRContext.h>
9+
#include <mlir/Pass/PassManager.h>
10+
#include <mlir/IR/Builders.h>
11+
12+
#include <unordered_map>
813

914
namespace jit {
1015

16+
using IdValueMap = std::unordered_map<id_type, ::mlir::Value>;
17+
1118
// initialize jit
1219
void init();
1320

1421
void ttt();
1522

23+
// create a constant integer with given value
24+
extern ::mlir::Value createI64(const ::mlir::Location & loc, ::mlir::OpBuilder & builder, int64_t val);
25+
1626
// A class to manage the MLIR business (compilation and execution).
1727
// Just a stub for now, will need to be extended with paramters and maybe more.
1828
class JIT {

src/jit/mlir.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static ::mlir::Type makeSignlessType(::mlir::Type type)
5757
return type;
5858
}
5959

60-
auto createI64(const ::mlir::Location & loc, ::mlir::OpBuilder & builder, int64_t val)
60+
::mlir::Value createI64(const ::mlir::Location & loc, ::mlir::OpBuilder & builder, int64_t val)
6161
{
6262
auto attr = builder.getI64IntegerAttr(val);
6363
return builder.create<::mlir::arith::ConstantOp>(loc, attr).getResult();

0 commit comments

Comments
 (0)