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