This is an archive of the discontinued LLVM Phabricator instance.

[DirectX backend] Support global ctor for DXILBitcodeWriter.
ClosedPublic

Authored by python3kgae on Sep 5 2022, 1:02 AM.

Details

Summary
  1. Save typed pointer type for GlobalVariable/Function instead of the ObjectType. This will allow use GlobalVariable/Function as value.
  2. Save target type for global ctors for Constant.
  3. In DXILBitcodeWriter::getTypeID, check PointerMap first for Constant case.

Diff Detail

Event Timeline

python3kgae created this revision.Sep 5 2022, 1:02 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 5 2022, 1:02 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
python3kgae requested review of this revision.Sep 5 2022, 1:02 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 5 2022, 1:02 AM

Use CallBase to cover both CallInst and Invoke

Recover patch overwrite by mistake.

beanz added inline comments.Sep 13 2022, 11:34 AM
llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
2121
llvm/lib/Target/DirectX/PointerTypeAnalysis.cpp
40

nit: please add braces here.

This is an example of the contrary point in the standard where readability is impacted by the braces. Since the first if has braces to improve readability we should keep braces on all the else blocks too.

96

This confuses me. RetTy starts as nullptr, if NewRetTy is also nullptr we set RetTy to nullptr? Unless I'm missing something this does nothing.

This changes the logic in the loop body, but not in a way that is clear to follow.

If I'm reading this logic correctly, I don't see how this function ever returns anything other than i8*. You enter the loop RetTy is nullptr. If NewRetTy is anything, you have a mismatch, and get i8*.

120

Is there a more generic way to do this? I feel like this more or less hard codes the type.

Add notes classifyGlobalCtorPointerType.

python3kgae marked 4 inline comments as done.Sep 13 2022, 2:43 PM
python3kgae added inline comments.
llvm/lib/Target/DirectX/PointerTypeAnalysis.cpp
96

Reverted.

120

Not sure how to support Constant with typed pointer type { i32, void ()*, i8* } and {
i32, float (float)*, i8* } map to same Constant with opaque pointer type
{i32, ptr, ptr} yet.
Mark as FIXME for now.

python3kgae marked 2 inline comments as done.

Add new line.

Remove hard coded type for global ctor.

beanz added a comment.Sep 23 2022, 7:58 AM

As an FYI (not something I expect you to tackle here), I just filed an issue to track writing an analysis printer for the pointer analysis pass (https://github.com/llvm/llvm-project/issues/57934). That would really help us debug and test this pass as it grows in complexity.

I have some other comments below mostly nitpicks about comment wording, but a few questions too. Overall looking good.

llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
353

I assume this is true for all llvm::GlobalObjects right?

Maybe:

/// getGlobalObjectValueTypeID - returns the element type for a GlobalObject
///
/// GlobalObject types are saved by PointerTypeAnalysis as pointers to the 
/// GlobalObject, but in the bitcode writer we need the pointer element type.
566

The point of this being higher up in the function was to early out and avoid the cost of the map lookup. With your new code, is there a way we can rewrite this check instead of moving it?

1229

You’re not actually looking up the abbreviation, but rather the enumerated type identifier.

Maybe something more like:

// Use getGlobalObjectValueTypeID to look up the enumerated type ID for Global Variable types.
llvm/lib/Target/DirectX/PointerTypeAnalysis.cpp
123

Issue please.

128

Can we move this up above the map lookup?

159
207–208

I understand needing functions classified first, but this will break if we ever have any other global variable that contains function pointers right?

python3kgae marked 7 inline comments as done.

Code cleanup to match comments.

beanz accepted this revision.Sep 28 2022, 9:30 AM

One small nit, otherwise looks good.

llvm/lib/Target/DirectX/PointerTypeAnalysis.cpp
108–109

nit: Why not just assign this to ArgTy? Temporary seems unnecessary now.

This revision is now accepted and ready to land.Sep 28 2022, 9:30 AM

Rebase and fix issue for nullptr.

This revision was automatically updated to reflect the committed changes.
python3kgae marked an inline comment as done.
beanz added a comment.Sep 29 2022, 9:59 AM

@python3kgae This breaks the unit tests for the PointerTypeAnalysis pass, so I've reverted it in rG5d4dd5357076de54d70f8621a39626b393a6e110.

Please ensure that all the in-tree tests pass before pushing.

python3kgae reopened this revision.Sep 29 2022, 11:08 AM

Reopen for unit test fail.

This revision is now accepted and ready to land.Sep 29 2022, 11:08 AM

Fix unit test.

beanz accepted this revision.Sep 30 2022, 11:19 AM