Not using cached constants when importing instructions may lead to
undesired results, as breaking dominance rules in the translated MLIR
module.
Signed-off-by: Victor Perez <victor.perez@codeplay.com>
Differential D149247
[mlir][llvm] Use cached constants when importing landingpad instructions victor-eds on Apr 26 2023, 2:35 AM. Authored by
Details Not using cached constants when importing instructions may lead to Signed-off-by: Victor Perez <victor.perez@codeplay.com>
Diff Detail
Event TimelineComment Actions Thanks for fixing! It definitely makes sense to use the cached value directly. Do you by chance understand why convertConstantExpr fails? It seems like there may be an issue with the Comment Actions Thanks for the quick reviews! I don't think that's a convertConstantExpr issue. Let me explain why this change is needed looking at the test: entry: ; Constant insertion point = nullptr call void @f0(ptr null) ; %0 = llvm.mlir.null : llvm.ptr. Constant insertion point = %0 call void @f1(i32 0) ; %1 = llvm.mlir.constant 0 : i32. Constant insertion point = %1 invoke void @f0(ptr null) to label %exit unwind label %catch catch: %lp = landingpad { ptr, i32 } catch ptr null ; No new constant created, but the call to convertConstantExpr updates the constant insertion point to the operation defining the queried constant (ptr null). Constant insertion point = %0 call void @f2({ptr, i32} {ptr null, i32 0}) ; Constant is defined as a series of llvm.insertvalue operations. These access the null and 0 constants defined before. 0 is defined after the insertion point of constants builder, so it breaks dominance (we'll have %n-1 = llvm.insertvalue %n, ...). resume {ptr, i32} %lp exit: ret void Maybe we can add an assert to convertConstantExpr checking that the queried constant is not in the map (!valueMapping.count(constant) to avoid this kind of issues? Right now we're only calling this function from convertGlobal (after clearing the map), convertValue and convertMedatadaValue (after checking it is not present in the map), so I'm assuming this shouldn't break anything. WDYT? Comment Actions
I think you are right here! The insertion point should only be updated if the constant does not have a mapping. Updating it may lead to an insertion point that does not point to the end of the constant section anymore. If then further constants are added things may break! I will prepare a revision for it. Using convertValue solves the problem but it still makes sense to make convertConstantExpr more robust IMO. Thanks for the explanation. Comment Actions You are right it is indeed only called from safe places. I ended up adding an assertion as a reminder if things change in the future https://reviews.llvm.org/D149253. |