[llvm-dev] Strange behavior when trying to use MDString and ConstantAsMetadata with MDNode

Dominik Montada via llvm-dev llvm-dev at lists.llvm.org
Thu Dec 3 04:18:22 PST 2020


Hi,

I stumbled across some strange behavior when trying to use MDString and 
ConstantAsMetadata. I was trying to pass some metadata to a target 
intrinsic using MetadataAsValue. To do that I first wrapped the Metadata 
in an MDNode and then used that MDNode with MetadataAsValue to pass it 
as an argument to the intrinsic. I used the following code to do that:

     llvm::Metadata *Meta;
     [...]
     llvm::MDNode *Node = llvm::MDNode::get(getLLVMContext(), Meta);
     Value *Val = llvm::MetadataAsValue::get(getLLVMContext(), Node);

Meta can either be MDString or ConstantAsMetadata:

     if (Foo)
       Meta = llvm::MDString::get(getLLVMContext(), SomeString)
     else
       Meta = llvm::ConstantAsMetadata::get(Builder.getInt32(SomeInt))

This works for MDString but breaks for ConstantAsMetadata when the 
intrinsic hits the backend. There, the IRTranslator uses the following 
code to translate metadata arguments:

     if (auto MD = dyn_cast<MetadataAsValue>(Arg.value())) {
       auto *MDN = dyn_cast<MDNode>(MD->getMetadata());
       if (!MDN) // This was probably an MDString.
         return false;
       MIB.addMetadata(MDN);
     }

If Meta was the MDString, this works fine. However if Meta is 
ConstantAsMetadata, the dynamic cast to MDNode fails, causing 
compilation to fail. I'm wondering why this is happening, since I am 
wrapping Meta in an MDNode. Although I can't test this with SelDAG, the 
code there is similar to the one IRTranslator. So this is not a problem 
with the IRTranslator, but rather some seemingly strange behavior of how 
Metadata appears to work.

By the way, if I wrap an MDString AND a ConstantAsMetadata in an MDNode, 
everything works as expected. This problem only occurs if I try to use a 
single ConstantAsMetadata with MDNode.

Can anybody tell me what's going on here?

Cheers,

Dominik

-- 
----------------------------------------------------------------------
Dominik Montada                   Email: dominik.montada at hightec-rt.com
HighTec EDV-Systeme GmbH          Phone: +49 681 92613 19
Europaallee 19                    Fax:   +49-681-92613-26
D-66113 Saarbrücken               WWW: http://www.hightec-rt.com

Managing Director: Vera Strothmann
Register Court: Saarbrücken, HRB 10445, VAT ID: DE 138344222

This e-mail may contain confidential and/or privileged information. If
you are not the intended recipient please notify the sender immediately
and destroy this e-mail. Any unauthorised copying, disclosure or
distribution of the material in this e-mail is strictly forbidden.
---

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 6822 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20201203/d3bd2c3c/attachment.bin>


More information about the llvm-dev mailing list