Before the Music of the Ainur and the creation of Arda took place, Ilúvatar and the Ainur resided in the Void; but soon, “the echo of the music went out into the Void, and it was not void” (The Silmarillion, ‘Ainulindalë’).
We are also told that Melkor begins to “interweave matters of his own imagining” and by the third attempt in fashioning the Music, Ilúvatar says to the Ainur:
This is your minstrelsy; and each of you shall find contained herein, amid the design that I set before you, all those things which it may seem that he himself devised or added.
-The Silmarillion, ‘Ainulindalë’
Perhaps Melkor’s own contribution to the Music, was caught up in the final “grand vision” of the world and these fragments became enmeshed with the whole creation.
Among such fragments could have resided the essence for Ungoliant’s formation and eventual descent into Arda.
However, it must be clearly stated that by no means does this imply that Melkor created a Being of his own; for we are told many times that he could only corrupt, and that the gift of life resided only in the Flame Imperishable, and hence in Ilúvatar.
But to a certain extent, Melkor did in fact corrupt the Music of the Ainur …
Ultimately, everything within the world (and outside of it) comes from Ilúvatar; and so did Ungoliant: either directly or (more conceivably) through the elements in Morgoth’s discord.
For whatever is contained within Arda (be it good or bad), was either designed so or corrupted. Ungoliant seems to have been neither and since “she descended from the darkness that lies about Arda” (The Silmarillion, ‘Of the Darkening of Valinor’; Chapter 8), one of the only explanations would have to be as a result of the Music of the Ainur.
We have seen how the purpose of the Music of the Ainur was to fill the void, and Ungoliant – being a spirit of void itself – seems to have been the complete opposite result of such a vision.
A reversal on the concept of the Music, in that, she herself was a product of the void which was made non-existent. An inversion of the “filling up of the void” – hence void itself.
A cloak of darkness she wove about them when Melkor and Ungoliant set forth; an Unlight, in which things seemed to be no more, and which eyes could not pierce, for it was void.
– The Silmarillion, ‘Of the Darkening of Valinor’; Chapter 8
She literally seems to be the equivalent of our own dark matter (physical substance that emits no light, and cannot be directly observed); or the concept of negative space within Art.
To use modern and identifying term, Ungoliant can be seen as being a black hole; an anomaly. An entity that is a by-product of the discord that weaved itself within the Music of the Ainur.
(The full article can be found on here: Ungoliant: A Fragment of Melkor’s Discord?)