This is an archive of the discontinued LLVM Phabricator instance.

[NFC][DirectX backend] Fix crash when emit_obj for DirectX backend.
ClosedPublic

Authored by python3kgae on Jul 31 2022, 2:16 PM.

Details

Summary

When emit-obj from clang directly, DirectX backend will hit assert caused by not initialize passes for AsmPrinter.
The fix will initialize the passes by calling createPassConfig.
Also ignore global variable which not has section in DXILAsmPrinter::emitGlobalVariable to avoid hit llvm_unreachable in DXILTargetObjectFile::SelectSectionForGlobal.

Diff Detail

Event Timeline

python3kgae created this revision.Jul 31 2022, 2:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 31 2022, 2:16 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
python3kgae requested review of this revision.Jul 31 2022, 2:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 31 2022, 2:16 PM
beanz added a comment.Aug 1 2022, 8:25 AM

At first glance this seems like the wrong solution. Initialization of the DirectX target should not be initializing common LLVM code.

Are the other LLVM library initializers not being called somewhere that is causing you to hit this issue?

beanz added a comment.Aug 1 2022, 9:19 AM

Okay, digging into this more. The issue with GCModuleInfoPass looks like a 12-ish year old bug where it was left off the list of passes initialized in InitializeCodeGen, the MachineOptimizationRemarkEmitterPass is handled there, so as long as clang is properly initializing LLVM CodeGen all you should need to do is add initializeGCModuleInfoPass to llvm::initializeCodeGen.

@bogner, does that seem right to you?

At first glance this seems like the wrong solution. Initialization of the DirectX target should not be initializing common LLVM code.

Are the other LLVM library initializers not being called somewhere that is causing you to hit this issue?

addPassesToGenerateCode is not called. Since we don't do real code gen.
That's where createPassConfig is called which calls initializeCodeGen.

Maybe we can initialize these passes for AsmPrinter in DirectXTargetMachine::addPassesToEmitFile instead of initalizeCodeGen which will initialize a lot of passes we don't need.

beanz added a comment.Aug 1 2022, 10:09 AM

The DirectX backend needs to be constructing a target pass config. The bug is that our override of addPassesToEmitFile isn't creating the pass config.

Call createPassConfig to make sure passes get initialized.
Skip Global Variable which not have section when emitGlobalVariable.

beanz added inline comments.Aug 1 2022, 4:45 PM
llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
75

this pass we should probably keep in the addPassesToEmitFile override. This needs to be done last before emitting an object file, so keeping it separate will reduce the likelihood of introducing errors.

Keep createDXILEmbedderPass in addPassesToEmitFile.

python3kgae marked an inline comment as done.Aug 1 2022, 4:58 PM
beanz accepted this revision.Aug 2 2022, 11:50 AM

LGTM.

Please fix the title to be brief and descriptive of the change, and add an appropriate change description before merging.

This revision is now accepted and ready to land.Aug 2 2022, 11:50 AM
python3kgae retitled this revision from [NFC][DirectX backend] Fix crash when emit_obj for DirectX backend. When emit-obj from clang directly, DirectX backend will hit assert caused by not initialize passes for AsmPrinter. to [NFC][DirectX backend] Fix crash when emit_obj for DirectX backend..Aug 2 2022, 12:05 PM
python3kgae edited the summary of this revision. (Show Details)
This revision was landed with ongoing or failed builds.Aug 2 2022, 12:09 PM
This revision was automatically updated to reflect the committed changes.