@@ -521,10 +521,14 @@ void ModuleImport::addDebugIntrinsic(llvm::CallInst *intrinsic) {
521521
522522static Attribute convertCGProfileModuleFlagValue (ModuleOp mlirModule,
523523 llvm::MDTuple *mdTuple) {
524- auto getFunctionSymbol = [&](const llvm::MDOperand &funcMDO) {
525- auto *f = cast<llvm::ValueAsMetadata>(funcMDO);
524+ auto getLLVMFunction =
525+ [&](const llvm::MDOperand &funcMDO) -> llvm::Function * {
526+ auto *f = cast_or_null<llvm::ValueAsMetadata>(funcMDO);
527+ // nullptr is a valid value for the function pointer.
528+ if (!f)
529+ return nullptr ;
526530 auto *llvmFn = cast<llvm::Function>(f->getValue ()->stripPointerCasts ());
527- return FlatSymbolRefAttr::get (mlirModule-> getContext (), llvmFn-> getName ()) ;
531+ return llvmFn;
528532 };
529533
530534 // Each tuple element becomes one ModuleFlagCGProfileEntryAttr.
@@ -535,9 +539,17 @@ static Attribute convertCGProfileModuleFlagValue(ModuleOp mlirModule,
535539 llvm::Constant *llvmConstant =
536540 cast<llvm::ConstantAsMetadata>(cgEntry->getOperand (2 ))->getValue ();
537541 uint64_t count = cast<llvm::ConstantInt>(llvmConstant)->getZExtValue ();
542+ auto *fromFn = getLLVMFunction (cgEntry->getOperand (0 ));
543+ auto *toFn = getLLVMFunction (cgEntry->getOperand (1 ));
544+ // FlatSymbolRefAttr::get(mlirModule->getContext(), llvmFn->getName());
538545 cgProfile.push_back (ModuleFlagCGProfileEntryAttr::get (
539- mlirModule->getContext (), getFunctionSymbol (cgEntry->getOperand (0 )),
540- getFunctionSymbol (cgEntry->getOperand (1 )), count));
546+ mlirModule->getContext (),
547+ fromFn ? FlatSymbolRefAttr::get (mlirModule->getContext (),
548+ fromFn->getName ())
549+ : nullptr ,
550+ toFn ? FlatSymbolRefAttr::get (mlirModule->getContext (), toFn->getName ())
551+ : nullptr ,
552+ count));
541553 }
542554 return ArrayAttr::get (mlirModule->getContext (), cgProfile);
543555}
@@ -570,7 +582,8 @@ LogicalResult ModuleImport::convertModuleFlagsMetadata() {
570582
571583 if (!valAttr) {
572584 emitWarning (mlirModule.getLoc ())
573- << " unsupported module flag value: " << diagMD (val, llvmModule.get ());
585+ << " unsupported module flag value for key '" << key->getString ()
586+ << " ' : " << diagMD (val, llvmModule.get ());
574587 continue ;
575588 }
576589
0 commit comments