@@ -88,6 +88,25 @@ Value globalStr(ConversionPatternRewriter &rewriter, Location loc, ModuleOp mod,
8888 ArrayRef<LLVM ::GEPArg>{0 , 0 }, LLVM ::GEPNoWrapFlags::inbounds);
8989}
9090
91+ void emitCheckedStatus (ConversionPatternRewriter &rewriter, Location loc, ModuleOp mod,
92+ StringRef name, ArrayRef<Type> paramTys, ValueRange args, StringRef what) {
93+ auto *ctx = rewriter.getContext ();
94+ Value rc = emitCall (rewriter, loc, mod, name, paramTys, i32Ty (ctx), args);
95+ Value msg = globalStr (rewriter, loc, mod, " transport_check_" , what);
96+ emitCall (rewriter, loc, mod, " __catalyst__transport__check" , {i32Ty (ctx), ptrTy (ctx)}, Type (),
97+ {rc, msg});
98+ }
99+
100+ Value emitCheckedSession (ConversionPatternRewriter &rewriter, Location loc, ModuleOp mod,
101+ StringRef name, ArrayRef<Type> paramTys, ValueRange args, StringRef what) {
102+ auto *ctx = rewriter.getContext ();
103+ Value s = emitCall (rewriter, loc, mod, name, paramTys, ptrTy (ctx), args);
104+ Value msg = globalStr (rewriter, loc, mod, " transport_session_" , what);
105+ emitCall (rewriter, loc, mod, " __catalyst__transport__check_session" , {ptrTy (ctx), ptrTy (ctx)},
106+ Type (), {s, msg});
107+ return s;
108+ }
109+
91110Value constInt (ConversionPatternRewriter &rewriter, Location loc, Type ty, int64_t v) {
92111 return LLVM::ConstantOp::create (rewriter, loc, ty, rewriter.getIntegerAttr (ty, v));
93112}
@@ -121,9 +140,9 @@ struct CreateLowering : public OpConversionPattern<CreateOp> {
121140 Value key = globalStr (rewriter, op.getLoc (), mod, " transport_key_" , op.getKey ());
122141 Value role =
123142 constInt (rewriter, op.getLoc (), i32Ty (ctx), static_cast <int64_t >(sessTy.getRole ()));
124- Value s = emitCall (rewriter, op.getLoc (), mod, " __catalyst__transport__create" ,
125- {ptrTy (ctx), ptrTy (ctx), i32Ty (ctx), ptrTy (ctx)}, ptrTy (ctx) ,
126- {lib, cfg, role, key});
143+ Value s = emitCheckedSession (rewriter, op.getLoc (), mod, " __catalyst__transport__create" ,
144+ {ptrTy (ctx), ptrTy (ctx), i32Ty (ctx), ptrTy (ctx)},
145+ {lib, cfg, role, key}, " create " );
127146 rewriter.replaceOp (op, s);
128147 return success ();
129148 }
@@ -143,9 +162,9 @@ template <typename OpT, bool Async> struct ConnectLoweringBase : public OpConver
143162 {adaptor.getSession (), peer, port});
144163 rewriter.replaceOp (op, r);
145164 } else {
146- emitCall (rewriter, op.getLoc (), mod, " __catalyst__transport__connect" ,
147- {ptrTy (ctx), ptrTy (ctx), IntegerType::get (ctx, 16 )}, i32Ty (ctx) ,
148- {adaptor.getSession (), peer, port});
165+ emitCheckedStatus (rewriter, op.getLoc (), mod, " __catalyst__transport__connect" ,
166+ {ptrTy (ctx), ptrTy (ctx), IntegerType::get (ctx, 16 )},
167+ {adaptor.getSession (), peer, port}, " connect " );
149168 rewriter.eraseOp (op);
150169 }
151170 return success ();
@@ -167,8 +186,8 @@ struct ExchangeKeysLoweringBase : public OpConversionPattern<OpT> {
167186 {ptrTy (ctx)}, i64Ty (ctx), {adaptor.getSession ()});
168187 rewriter.replaceOp (op, r);
169188 } else {
170- emitCall (rewriter, op.getLoc (), mod, " __catalyst__transport__exchange_keys" ,
171- {ptrTy (ctx)}, i32Ty (ctx), {adaptor.getSession ()});
189+ emitCheckedStatus (rewriter, op.getLoc (), mod, " __catalyst__transport__exchange_keys" ,
190+ {ptrTy (ctx)}, {adaptor.getSession ()}, " exchange_keys " );
172191 rewriter.eraseOp (op);
173192 }
174193 return success ();
@@ -182,8 +201,8 @@ struct AwaitLowering : public OpConversionPattern<AwaitOp> {
182201 LogicalResult matchAndRewrite (AwaitOp op, OpAdaptor adaptor,
183202 ConversionPatternRewriter &rewriter) const override {
184203 auto *ctx = op.getContext ();
185- emitCall (rewriter, op.getLoc (), moduleOf (op), " __catalyst__transport__await" , { i64Ty (ctx)} ,
186- i32Ty (ctx), {adaptor.getToken ()});
204+ emitCheckedStatus (rewriter, op.getLoc (), moduleOf (op), " __catalyst__transport__await" ,
205+ { i64Ty (ctx)} , {adaptor.getToken ()}, " await " );
187206 rewriter.eraseOp (op);
188207 return success ();
189208 }
@@ -196,8 +215,9 @@ struct EstablishChannelLowering : public OpConversionPattern<EstablishChannelOp>
196215 auto *ctx = op.getContext ();
197216 Value transport =
198217 globalStr (rewriter, op.getLoc (), moduleOf (op), " transport_kind_" , op.getTransport ());
199- emitCall (rewriter, op.getLoc (), moduleOf (op), " __catalyst__transport__establish_channel" ,
200- {ptrTy (ctx), ptrTy (ctx)}, i32Ty (ctx), {adaptor.getSession (), transport});
218+ emitCheckedStatus (rewriter, op.getLoc (), moduleOf (op),
219+ " __catalyst__transport__establish_channel" , {ptrTy (ctx), ptrTy (ctx)},
220+ {adaptor.getSession (), transport}, " establish_channel" );
201221 rewriter.eraseOp (op);
202222 return success ();
203223 }
@@ -210,8 +230,9 @@ struct SetCoprocessorFnLowering : public OpConversionPattern<SetCoprocessorFnOp>
210230 auto *ctx = op.getContext ();
211231 ModuleOp mod = moduleOf (op);
212232 Value sym = globalStr (rewriter, op.getLoc (), mod, " transport_coproc_fn_" , op.getSymbol ());
213- emitCall (rewriter, op.getLoc (), mod, " __catalyst__transport__set_coprocessor_fn" ,
214- {ptrTy (ctx), ptrTy (ctx)}, i32Ty (ctx), {adaptor.getSession (), sym});
233+ emitCheckedStatus (rewriter, op.getLoc (), mod, " __catalyst__transport__set_coprocessor_fn" ,
234+ {ptrTy (ctx), ptrTy (ctx)}, {adaptor.getSession (), sym},
235+ " set_coprocessor_fn" );
215236 rewriter.eraseOp (op);
216237 return success ();
217238 }
@@ -225,9 +246,10 @@ struct SetMessageSizesLowering : public OpConversionPattern<SetMessageSizesOp> {
225246 Value idx = constInt (rewriter, op.getLoc (), i32Ty (ctx), op.getWorkItemIdx ());
226247 Value inB = constInt (rewriter, op.getLoc (), i64Ty (ctx), op.getInBytes ());
227248 Value outB = constInt (rewriter, op.getLoc (), i64Ty (ctx), op.getOutBytes ());
228- emitCall (rewriter, op.getLoc (), moduleOf (op), " __catalyst__transport__set_message_sizes" ,
229- {ptrTy (ctx), i32Ty (ctx), i64Ty (ctx), i64Ty (ctx)}, i32Ty (ctx),
230- {adaptor.getSession (), idx, inB, outB});
249+ emitCheckedStatus (rewriter, op.getLoc (), moduleOf (op),
250+ " __catalyst__transport__set_message_sizes" ,
251+ {ptrTy (ctx), i32Ty (ctx), i64Ty (ctx), i64Ty (ctx)},
252+ {adaptor.getSession (), idx, inB, outB}, " set_message_sizes" );
231253 rewriter.eraseOp (op);
232254 return success ();
233255 }
@@ -281,9 +303,10 @@ struct StagePayloadLowering : public OpConversionPattern<StagePayloadOp> {
281303 auto [srcPtr, bytes] =
282304 memrefPtrAndBytes (rewriter, op.getLoc (), adaptor.getPayload (), memTy);
283305 Value decoderId = constInt (rewriter, op.getLoc (), i32Ty (ctx), op.getDecoderId ());
284- emitCall (rewriter, op.getLoc (), moduleOf (op), " __catalyst__transport__stage_payload" ,
285- {ptrTy (ctx), ptrTy (ctx), i64Ty (ctx), i32Ty (ctx)}, i32Ty (ctx),
286- {adaptor.getSession (), srcPtr, bytes, decoderId});
306+ emitCheckedStatus (rewriter, op.getLoc (), moduleOf (op),
307+ " __catalyst__transport__stage_payload" ,
308+ {ptrTy (ctx), ptrTy (ctx), i64Ty (ctx), i32Ty (ctx)},
309+ {adaptor.getSession (), srcPtr, bytes, decoderId}, " stage_payload" );
287310 rewriter.eraseOp (op);
288311 return success ();
289312 }
@@ -295,8 +318,8 @@ struct PostLowering : public OpConversionPattern<PostOp> {
295318 ConversionPatternRewriter &rewriter) const override {
296319 auto *ctx = op.getContext ();
297320 Value idx = constInt (rewriter, op.getLoc (), i32Ty (ctx), op.getWorkItemIdx ());
298- emitCall (rewriter, op.getLoc (), moduleOf (op), " __catalyst__transport__post" ,
299- {ptrTy (ctx), i32Ty (ctx)}, i32Ty (ctx), {adaptor.getSession (), idx});
321+ emitCheckedStatus (rewriter, op.getLoc (), moduleOf (op), " __catalyst__transport__post" ,
322+ {ptrTy (ctx), i32Ty (ctx)}, {adaptor.getSession (), idx}, " post " );
300323 rewriter.eraseOp (op);
301324 return success ();
302325 }
@@ -319,9 +342,9 @@ struct CollectLowering : public OpConversionPattern<CollectOp> {
319342 return rewriter.notifyMatchFailure (op, " collect dest must have identity layout" );
320343 }
321344 auto [dstPtr, bytes] = memrefPtrAndBytes (rewriter, op.getLoc (), adaptor.getDest (), memTy);
322- emitCall (rewriter, op.getLoc (), mod, " __catalyst__transport__collect" ,
323- {ptrTy (ctx), ptrTy (ctx), i64Ty (ctx)}, i32Ty (ctx) ,
324- {adaptor.getSession (), dstPtr, bytes});
345+ emitCheckedStatus (rewriter, op.getLoc (), mod, " __catalyst__transport__collect" ,
346+ {ptrTy (ctx), ptrTy (ctx), i64Ty (ctx)},
347+ {adaptor.getSession (), dstPtr, bytes}, " collect " );
325348 rewriter.eraseOp (op);
326349 return success ();
327350 }
@@ -364,8 +387,9 @@ struct GetSessionLowering : public OpConversionPattern<GetSessionOp> {
364387 Value role =
365388 constInt (rewriter, op.getLoc (), i32Ty (ctx), static_cast <int64_t >(sessTy.getRole ()));
366389 Value key = globalStr (rewriter, op.getLoc (), mod, " transport_key_" , op.getKey ());
367- Value s = emitCall (rewriter, op.getLoc (), mod, " __catalyst__transport__get_session" ,
368- {i32Ty (ctx), ptrTy (ctx)}, ptrTy (ctx), {role, key});
390+ Value s =
391+ emitCheckedSession (rewriter, op.getLoc (), mod, " __catalyst__transport__get_session" ,
392+ {i32Ty (ctx), ptrTy (ctx)}, {role, key}, " get_session" );
369393 rewriter.replaceOp (op, s);
370394 return success ();
371395 }
0 commit comments