This is an archive of the discontinued LLVM Phabricator instance.

[mlir][llvm] Use cached constants when importing landingpad instructions
ClosedPublic

Authored by victor-eds on Apr 26 2023, 2:35 AM.

Details

Summary

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>

Diff Detail

Event Timeline

victor-eds created this revision.Apr 26 2023, 2:35 AM
Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
victor-eds requested review of this revision.Apr 26 2023, 2:35 AM
zero9178 accepted this revision.Apr 26 2023, 2:41 AM

LGTM!

This revision is now accepted and ready to land.Apr 26 2023, 2:41 AM
gysit accepted this revision.Apr 26 2023, 4:02 AM

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
convertConstantExpr function itself?

victor-eds added a comment.EditedApr 26 2023, 4:42 AM

@gysit, @zero9178,

Thanks for the quick reviews!

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
convertConstantExpr function itself?

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?

gysit added a comment.Apr 26 2023, 4:56 AM

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?

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.

gysit added a comment.Apr 26 2023, 5:52 AM

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.