@@ -130,13 +130,23 @@ bool RuntimeLibcallsInfo::darwinHasExp10(const Triple &TT) {
130130 }
131131}
132132
133+ // / TODO: There is really no guarantee that sizeof(size_t) is equal to the index
134+ // / size of the default address space. This matches TargetLibraryInfo and should
135+ // / be kept in sync.
136+ static IntegerType *getSizeTType (LLVMContext &Ctx, const DataLayout &DL) {
137+ return DL.getIndexType (Ctx, /* AddressSpace=*/ 0 );
138+ }
139+
133140std::pair<FunctionType *, AttributeList>
134141RuntimeLibcallsInfo::getFunctionTy (LLVMContext &Ctx, const Triple &TT,
135142 const DataLayout &DL,
136143 RTLIB::LibcallImpl LibcallImpl) const {
144+ // TODO: NoCallback probably unsafe in general
137145 static constexpr Attribute::AttrKind CommonFnAttrs[] = {
138146 Attribute::MustProgress, Attribute::NoCallback, Attribute::NoFree,
139147 Attribute::NoSync, Attribute::NoUnwind, Attribute::WillReturn};
148+ static constexpr Attribute::AttrKind MemoryFnAttrs[] = {
149+ Attribute::MustProgress, Attribute::NoUnwind, Attribute::WillReturn};
140150 static constexpr Attribute::AttrKind CommonPtrArgAttrs[] = {
141151 Attribute::NoAlias, Attribute::WriteOnly, Attribute::NonNull};
142152
@@ -182,6 +192,71 @@ RuntimeLibcallsInfo::getFunctionTy(LLVMContext &Ctx, const Triple &TT,
182192
183193 return {FunctionType::get (RetTy, {ScalarTy}, false ), Attrs};
184194 }
195+ case RTLIB::impl_malloc:
196+ case RTLIB::impl_calloc: {
197+ AttrBuilder FuncAttrBuilder (Ctx);
198+ for (Attribute::AttrKind Attr : MemoryFnAttrs)
199+ FuncAttrBuilder.addAttribute (Attr);
200+ FuncAttrBuilder.addAttribute (Attribute::NoFree);
201+
202+ AllocFnKind AllocKind = AllocFnKind::Alloc;
203+ if (LibcallImpl == RTLIB::impl_malloc)
204+ AllocKind |= AllocFnKind::Uninitialized;
205+
206+ // TODO: Set memory attribute
207+ FuncAttrBuilder.addAllocKindAttr (AllocKind);
208+ FuncAttrBuilder.addAttribute (" alloc-family" , " malloc" );
209+ FuncAttrBuilder.addAllocSizeAttr (0 , LibcallImpl == RTLIB::impl_malloc
210+ ? std::nullopt
211+ : std::make_optional (1 ));
212+
213+ AttributeList Attrs;
214+ Attrs = Attrs.addFnAttributes (Ctx, FuncAttrBuilder);
215+
216+ {
217+ AttrBuilder ArgAttrBuilder (Ctx);
218+ for (Attribute::AttrKind AK : CommonPtrArgAttrs)
219+ ArgAttrBuilder.addAttribute (AK);
220+
221+ Attrs = Attrs.addRetAttribute (Ctx, Attribute::NoUndef);
222+ Attrs = Attrs.addRetAttribute (Ctx, Attribute::NoAlias);
223+ Attrs = Attrs.addParamAttribute (Ctx, 0 , Attribute::NoUndef);
224+ if (LibcallImpl == RTLIB::impl_calloc)
225+ Attrs = Attrs.addParamAttribute (Ctx, 1 , Attribute::NoUndef);
226+ }
227+
228+ IntegerType *SizeT = getSizeTType (Ctx, DL);
229+ PointerType *PtrTy = PointerType::get (Ctx, 0 );
230+ SmallVector<Type *, 2 > ArgTys = {SizeT};
231+ if (LibcallImpl == RTLIB::impl_calloc)
232+ ArgTys.push_back (SizeT);
233+
234+ return {FunctionType::get (PtrTy, ArgTys, false ), Attrs};
235+ }
236+ case RTLIB::impl_free: {
237+ // TODO: Set memory attribute
238+ AttrBuilder FuncAttrBuilder (Ctx);
239+ for (Attribute::AttrKind Attr : MemoryFnAttrs)
240+ FuncAttrBuilder.addAttribute (Attr);
241+
242+ FuncAttrBuilder.addAllocKindAttr (AllocFnKind::Free);
243+ FuncAttrBuilder.addAttribute (" alloc-family" , " malloc" );
244+
245+ AttributeList Attrs;
246+ Attrs = Attrs.addFnAttributes (Ctx, FuncAttrBuilder);
247+
248+ {
249+ AttrBuilder ArgAttrBuilder (Ctx);
250+ ArgAttrBuilder.addAttribute (Attribute::NoUndef);
251+ ArgAttrBuilder.addAttribute (Attribute::AllocatedPointer);
252+ ArgAttrBuilder.addCapturesAttr (CaptureInfo::none ());
253+ Attrs = Attrs.addParamAttributes (Ctx, 0 , ArgAttrBuilder);
254+ }
255+
256+ return {FunctionType::get (Type::getVoidTy (Ctx), {PointerType::get (Ctx, 0 )},
257+ false ),
258+ Attrs};
259+ }
185260 case RTLIB::impl_sqrtf:
186261 case RTLIB::impl_sqrt: {
187262 AttrBuilder FuncAttrBuilder (Ctx);
0 commit comments